142 - 40 组合总和2
题目
解答
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if not candidates:
return []
res = []
tmp = []
candidates.sort()
def helper(begin, target, candidates):
if target == 0:
res.append(tmp[:])
for i in range(begin, len(candidates)):
if i > 0 and candidates[i] == candidates[i-1]:
continue
diff = target - candidates[i]
if diff < 0:
break
tmp.append(candidates[i])
newCand = candidates[:]
newCand.pop(i)
helper(i, diff, newCand)
tmp.pop()
helper(0, target, candidates)
return resLast updated