Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit ac99b7d

Browse files
committed
created solution for problem58
1 parent 0472d92 commit ac99b7d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

problem58/Solution.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
3+
ans = []
4+
combo = []
5+
6+
def backtrack(size, sum):
7+
if size == k:
8+
if sum == n:
9+
ans.append(combo.copy())
10+
return True
11+
return False
12+
13+
strt = 1 if not combo else combo[-1]+1
14+
15+
for i in range(strt, 10):
16+
combo.append(i)
17+
sum += i
18+
found = backtrack(size+1, sum)
19+
sum -= i
20+
combo.pop(-1)
21+
if found: break
22+
return False
23+
24+
backtrack(0, 0)
25+
return ans

0 commit comments

Comments
 (0)