-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.py
More file actions
42 lines (40 loc) · 1.59 KB
/
CombinationSum.py
File metadata and controls
42 lines (40 loc) · 1.59 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
32
33
34
35
36
37
38
39
40
41
42
#https://leetcode.com/problems/combination-sum-ii/description/
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
self.candidates = candidates
self.size = len(candidates)
self.rtype = []
def findsums(valuesum,valuelist, index, target):
complete = False
if valuesum > target:
return
elif valuesum == target:
if valuelist not in self.rtype:
self.rtype.append(valuelist)
return
while index < self.size:
if valuesum + self.candidates[index] == target:
good = valuelist[:]
good.append(self.candidates[index])
if good not in self.rtype:
self.rtype.append(good )
return
elif valuesum + self.candidates[index] < target:
less = valuelist[:]
less.append(self.candidates[index])
findsums(valuesum + self.candidates[index],less,index + 1,target)
elif valuesum + self.candidates[index] > target:
great = valuelist[:]
return
index += 1
count = 0
self.candidates.sort()
while count < self.size :
findsums(self.candidates[count],[self.candidates[count]],count + 1,target)
count += 1
return self.rtype