class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
track = []
def back_track(start, track, target, candidates):
if sum(track) == target:
res.append(track[:])
return
for choice in range(start, len(candidates)):
track.append(candidates[choice])
if sum(track) > target:
track.pop()
continue
back_track(choice, track, target, candidates) # 注意这里每次传的是choice
track.pop()
back_track(0, track, target, candidates)
return res
注意每次回溯时都只考虑候选数列中第i项及i以后的,所以会把choice传入
若每次考虑的内容都是当前数以后的,则会把start+1传入(子集枚举,排列等)