-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39.py
25 lines (19 loc) · 814 Bytes
/
39.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
# Problem link: https://leetcode.com/problems/combination-sum/
class Solution:
def findCombinations(self, curSum, chosen, target, candidates):
if curSum > target:
return []
if curSum == target:
return [chosen]
res = []
for cand in candidates:
if chosen and cand < chosen[-1]:
continue
new_chosen = chosen + [cand]
combinations = self.findCombinations(curSum + cand, new_chosen, target, candidates)
if combinations:
res += combinations
return res
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
return self.findCombinations(0, [], target, candidates)