Leetcode39-组合总和
思路:深度优先遍历/深度优先搜索
-----------------------------------------------------------------------------------------
代码:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
def dfs(index, tmp_sum, tmp_list):
if tmp_sum == target:
res.append(tmp_list)
return
if tmp_sum > target:
return
for j in range(index, len(candidates)):
dfs(j, tmp_sum + candidates[j], tmp_list + [candidates[j]])
dfs(0, 0, [])
return res