链接: 组合总和
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
self.dfs(candidates, target, [], res, 0)
return res
def dfs(self, candidates, target, path, ans, begin):
if target == 0:
ans.append(path)
return
for i in range(begin, len(candidates)):
num = candidates[i]
if num <= target:
self.dfs(candidates, target-num, path+[num], ans, i+1)