-
Notifications
You must be signed in to change notification settings - Fork 0
/
39.combination-sum.py
39 lines (32 loc) · 1.14 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
#
# @lc app=leetcode id=39 lang=python3
#
# [39] Combination Sum
#
# @lc code=start
# [7,3,2]\n18
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# impossible_target = set() # 前面不行不代表后面不行
candidates.sort()
def findAllCombinationForTarget(target, startIndex):
# if target in impossible_target:
# return []
res = []
for i in range(startIndex, len(candidates)):
next_target = target - candidates[i]
sub_res = []
if next_target > 0:
sub_res = findAllCombinationForTarget(next_target, i)
elif next_target == 0:
sub_res = [[]]
else:
break
for lst in sub_res:
lst.append(candidates[i])
res.append(lst)
# if not res:
# impossible_target.add(target)
return res
return findAllCombinationForTarget(target, 0)
# @lc code=end