-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path039. Combination Sum.py
35 lines (27 loc) · 958 Bytes
/
039. 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
class Solution(object):
def combinationSumHelper(self, remCandidates, remTarget, partialRes, results):
if remTarget==0:
results.append( list(partialRes) )
return
if remTarget<0:
return
if not remCandidates:
return
currCand = remCandidates[-1]
# use currCand
partialRes.append( currCand )
self.combinationSumHelper( remCandidates, remTarget-currCand, partialRes, results)
partialRes.pop()
# don't use currCand
remCandidates.pop()
self.combinationSumHelper( remCandidates, remTarget, partialRes, results)
remCandidates.append( currCand )
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
ret = []
self.combinationSumHelper(candidates, target, [], ret)
return ret