-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path39. Combination Sum.py
More file actions
38 lines (35 loc) · 912 Bytes
/
39. Combination Sum.py
File metadata and controls
38 lines (35 loc) · 912 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author: 57ing
@contact: black8clock@gmail.com
@file: 39. Combination Sum.py
@time: 2018/3/7 10:15
'''
class Solution:
def __init__(self):
self.res = []
self.re = []
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
self.run(candidates,target,len(candidates)-1)
return (self.res)
def run(self,candidates,target,i):
n = 0
while target > 0:
if i>=1:
self.run(candidates,target,i-1)
target -= candidates[i]
self.re.append(candidates[i])
n += 1
if target == 0:
self.res.append(self.re[:])
for i in range(n):
self.re.pop()
so = Solution()
so.combinationSum([1,2,3,6,7],7)
print(so.res)