-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_39.py
44 lines (36 loc) · 1.31 KB
/
ex_39.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from copy import deepcopy
class Solution:
"""
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target),
find all unique combinations in candidates where the candidate numbers sums to target.
"""
def combinationFind(self, results, combination, candidates, target, ind):
"""
:type results: List[List[int]]
:type combination: List[int]
:type candidates: List[int]
:type target: int
:type ind: int
"""
if target == 0:
results.append(deepcopy(combination))
for i in range(ind, len(candidates)):
if candidates[i] > target:
break
combination.append(candidates[i])
self.combinationFind(results, combination, candidates, target - candidates[i], i)
combination.pop(len(combination) - 1)
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates = sorted(candidates)
results = []
combination = []
self.combinationFind(results, combination, candidates, target, 0)
return results
if __name__ == "__main__":
result = Solution()
print(result.combinationSum([2, 3, 6, 7], 7))