题目地址:https://leetcode-cn.com/problems/combination-sum-ii/
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
if not candidates: return []
n = len(candidates)
candidates.sort()
res = []
def helper(i, tmp, target):
if target == 0:
res.append(tmp)
return
if(i == n or target < candidates[i]):
return
for j in range(i, n):
if(j > i and candidates[j] == candidates[j - 1]):
continue
helper(j + 1, tmp + [candidates[j]], target - candidates[j])
helper(0, [], target)
return res
还是看了吴彦祖的题解,thanks