-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path40. Combination Sum II.py
More file actions
31 lines (27 loc) · 1.09 KB
/
40. Combination Sum II.py
File metadata and controls
31 lines (27 loc) · 1.09 KB
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
class Solution(object):
def combinationSum2(self, candidates, target):
candidates.sort()
candidates.reverse()
self.results = []
def test(candidates, target, combination):
for i in range(len(candidates)):
if candidates[i] > target:
continue
# print(combination, target)
combination.append(candidates[i])
if candidates[i] == target:
temp = list(combination)
temp.sort()
# print('temp: ' + str(temp))
if temp not in self.results:
self.results.append(temp)
elif candidates[i] < target:
test(candidates[i+1:], target-candidates[i], combination)
if combination:
del combination[-1]
test(candidates, target, [])
return self.results
if __name__ == '__main__':
candidates = [2,5,2,1,2]
target = 5
print(Solution().combinationSum2(candidates, target))