-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.py
More file actions
37 lines (24 loc) · 751 Bytes
/
39.py
File metadata and controls
37 lines (24 loc) · 751 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
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
def dfs(i, cur, total):
if total == target:
res.append(list(cur))
return
if i >= len(candidates) or total > target:
return
# Choose to include candidates at i
cur.append(candidates[i])
dfs(i, cur, sum(cur))
cur.pop()
dfs(i + 1, cur, sum(cur))
dfs(0, [], 0)
return res
s = Solution()
# print(s.combinationSum([2,3,6,7], 7))
print(s.combinationSum([18,34,2,16,25,6,35], 40))