-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39. Combination Sum.py
43 lines (39 loc) · 1.35 KB
/
39. Combination Sum.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
import copy
class Solution(object):
def seeksolutino(self,candidates,target,index):
result = []
if len(candidates) <= index:
return result
#cur_value = candidates[0]
cur_value = candidates[index]
max = target / cur_value
#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:
result.append(re_tmp)
return result
next_tar = target - i * cur_value
re = self.seeksolutino(candidates,next_tar,index + 1)
for j in range(0,len(re)):
re_tmp.extend(re[j])
result.append(re_tmp)
re_tmp = []
for k in range(0, i):
re_tmp.append(cur_value)
return result
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
re = self.seeksolutino(candidates,target,0)
return re
if __name__ == "__main__":
solution = Solution()
re = solution.combinationSum([44,40,27,30,23,36,49,22,43,24,28,34,45,26,38,41,39,47,32,35,33,42,20,37,21,25,48,46],67)
print re