添加链接描述
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def backtrack(path,target,res,index):
if target==0:
res.append(path[:])
return
if target<0:
return
for i in range(index,len(candidates)):
if target>=candidates[i]:
path.append(candidates[i])
backtrack(path,target-candidates[i],res,i)
path.pop()
res=[]
backtrack([],target,res,0)
return res
思路:
- 递归与回溯,一眼树形结构,找到想要的节点就可以