Untitled
unknown
plain_text
a year ago
1.2 kB
2
Indexable
Never
class Solution: def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: if sum(candidates) < target: return [] candidates.sort() seen = set() length = len(candidates) def findSum(index, target, current): nonlocal seen if index >= length: return False for i in range(index, length): if i != index and candidates[i] == candidates[i - 1]: continue if candidates[i] == target: seen.add(tuple(current + [candidates[i]])) elif candidates[i] > target: break else: answer = findSum(i + 1, target - candidates[i], current + [candidates[i]]) if answer: seen.add(tuple(answer)) for i in range(length): if candidates[i] > target: break findSum(i, target, []) result = [] for t in seen: result.append(list(t)) return result