class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
n = len(candidates)
candidates = sorted(candidates)
res = []
track = []
trackSum = 0
def backtrack(start, track):
nonlocal trackSum
if trackSum == target:
res.append(track[:])
return
elif trackSum > target:
return
for cho in range(start, n):
if cho > start and candidates[cho] == candidates[cho-1]:
continue
track.append(candidates[cho])
trackSum += candidates[cho]
backtrack(cho+1, track)
trackSum -= candidates[cho]
track.pop()
backtrack(0, track)
return res
|