141 - 39 组合总和
题目
解答
解法1
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
if len(candidates) == 0:
return []
res = []
path = []
candidates.sort()
def helper(candidates, begin, target):
if target == 0:
res.append(path[:])
for index in range(begin, len(candidates)):
residue = target - candidates[index]
if residue < 0:
break
path.append(candidates[index])
helper(candidates, index, residue)
path.pop()
helper(candidates, 0, target)
return res解法2
Last updated