-
Notifications
You must be signed in to change notification settings - Fork 0
/
039.py
39 lines (35 loc) · 1.13 KB
/
039.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
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
self.dfs(candidates, target, 0, [], res)
return res
def dfs(self, nums, target, index, path, res):
if target < 0:
return
if target == 0:
res.append(path)
for i in range(index, len(nums)):
self.dfs(nums, target-nums[i], i, path+[nums[i]], res)
# must konw why this answer returns unique solution
# using inner function to avoid parameter sending, a little bit faster.
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
def dfs(t, i, path, res):
if t < 0:
return
if t == 0:
res.append(path)
for k in range(i, len(candidates)):
dfs(t-candidates[k], k, path+[candidates[k]], res)
dfs(target, 0, [], res)
return res