-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path039_CombinationSum.py
More file actions
36 lines (21 loc) · 1.05 KB
/
Copy path039_CombinationSum.py
File metadata and controls
36 lines (21 loc) · 1.05 KB
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
class Solution:
def combinationSum(self, candidates, target):
result = []
tmp_list = []
self.dfs(candidates, tmp_list, target, result)
return result
def dfs(self, candidates, path, diff, res):
if diff == 0:
res.append(path)
return
elif diff < 0:
return
else:
for i in range(len(candidates)):
self.dfs(candidates[i:], path + [candidates[i]], diff - candidates[i], res)
#有个问题是我把diff == 0 和 diff<0 放到了for 里面,但是结果就不行,不知道为什么
print(Solution().combinationSum(candidates = [2,3,6,7], target = 7)) #[[2,2,3],[7]]
print(Solution().combinationSum(candidates = [2,3,5], target = 8)) #[[2,2,2,2],[2,3,3],[3,5]]
print(Solution().combinationSum(candidates = [2], target = 1)) #[]
print(Solution().combinationSum(candidates = [1], target = 1)) #[[1]]
print(Solution().combinationSum(candidates = [1], target = 2)) #[[1,1]]