-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40. Combination Sum II.py
59 lines (56 loc) · 1.93 KB
/
40. Combination Sum II.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution(object):
def seeksolutino(self,candidates,target,index,numOfdict):
result = []
if len(candidates) <= index:
return result
#cur_value = candidates[0]
cur_value = candidates[index]
max = target / cur_value
if max > numOfdict[candidates[index]]:
max = numOfdict[candidates[index]]
#can_tmp = copy.deepcopy(candidates)
#del(can_tmp[0])
for i in range(0,max + 1):
re_tmp = []
for j in range(0, i):
re_tmp.append(cur_value)
if target == i * cur_value:
if re_tmp not in result:
result.append(re_tmp)
return result
next_tar = target - i * cur_value
re = self.seeksolutino(candidates,next_tar,index + 1,numOfdict)
for j in range(0,len(re)):
re_tmp.extend(re[j])
re_tmp.sort()
if re_tmp not in result:
result.append(re_tmp)
re_tmp = []
for k in range(0, i):
re_tmp.append(cur_value)
return result
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
numOfdict = dict()
for i in range(0,len(candidates)):
if numOfdict.has_key(candidates[i]):
numOfdict[candidates[i]] = numOfdict[candidates[i]] + 1
else:
numOfdict[candidates[i]] = 1
candidates = list(set(candidates))
re = self.seeksolutino(candidates, target, 0, numOfdict)
'''
new_re = []
for ele in re:
if ele not in new_re:
new_re.append(ele)
'''
return re
if __name__ == "__main__":
solution = Solution()
re = solution.combinationSum2([10,1,2,7,6,1,5],8)
print re