class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ans = []
combination = []
cur_total = 0
candidates.sort()
// print(candidates)
def backtracking(idx,cur_total):
if cur_total == target:
ans.append(copy.deepcopy(combination))
return
// lists of choice
for index in range(idx,len(candidates)):
if candidates[index] + cur_total <= target:
// do choice
combination.append(candidates[index])
// backtracking
backtracking(index,cur_total+candidates[index])
// cancel choice
combination.pop()
else:
break
backtracking(0,0)
return ans
刷题记录:组合总和
最新推荐文章于 2024-07-25 20:26:57 发布