classSolution: defcombinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: res, A = [], [] candidates.sort() defdfs(b, s): if s == 0: res.append(A[:]) return for i inrange(b, len(candidates)): x = candidates[i] if x > s: break if i > b and x == candidates[i - 1]: continue A.append(x) dfs(i + 1, s - x) A.pop() dfs(0, target) return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defcombinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: res = [] candidates.sort() defdfs(b, s, A): if s == 0: res.append(A) return for i inrange(b, len(candidates)): x = candidates[i] if x > s: break if i > b and x == candidates[i - 1]: continue dfs(i + 1, s - x, A + [x]) dfs(0, target, []) return res