forked from shichao-an/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
21 lines (20 loc) · 679 Bytes
/
solution.py
File metadata and controls
21 lines (20 loc) · 679 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
# @param candidates, a list of integers
# @param target, integer
# @return a list of lists of integers
def combinationSum(self, candidates, target):
res = []
cand = []
candidates.sort()
self.combination_sum(candidates, cand, target, res)
return res
def combination_sum(self, candidates, cand, target, res):
if target < 0:
return
elif target == 0:
res.append(cand[:])
else:
for i, c in enumerate(candidates):
cand.append(c)
self.combination_sum(candidates[i:], cand, target - c, res)
cand.pop()