Skip to content

Commit 45ff2a2

Browse files
committed
combination sum solution
1 parent 66335b3 commit 45ff2a2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

combination-sum/devyejin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:
3+
4+
def backtracking(start, total, result):
5+
if total == target:
6+
answer.append(result[:])
7+
return
8+
9+
if sum(result) > target:
10+
return
11+
12+
for i in range(start, len(candidates)):
13+
result.append(candidates[i])
14+
backtracking(i, total + candidates[i], result)
15+
result.pop()
16+
17+
answer = []
18+
backtracking(0, 0, [])
19+
return answer
20+

0 commit comments

Comments
 (0)