Skip to content

Commit ca024ee

Browse files
committed
39. Combination Sum Solution
1 parent ce712fc commit ca024ee

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

combination-sum/doh6077.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
3+
def solve(self, candidates, remsum, cur, res, idx) :
4+
if remsum == 0 :
5+
res.append(list(cur))
6+
return
7+
8+
if remsum < 0 or idx >= len(candidates) :
9+
return
10+
cur.append(candidates[idx])
11+
self.solve(candidates, remsum-candidates[idx],cur,res,idx)
12+
cur.pop()
13+
self.solve(candidates,remsum,cur,res,idx+1)
14+
15+
16+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
17+
cur = []
18+
res = []
19+
self.solve(candidates,target,cur,res,0)
20+
return res

0 commit comments

Comments
 (0)