-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum_3.py
43 lines (34 loc) · 1 KB
/
combination_sum_3.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
36
37
38
39
40
41
42
43
"""
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def dfs(start, summ):
if summ > n or len(sol) > k:
return
if summ == n and len(sol) == k:
ans.append(sol.copy())
return
for i in range(start, 10):
sol.append(i)
dfs(i+1, i + summ)
sol.pop()
sol = []
ans = []
dfs(1,0)
return ans
"""
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def dfs(start, summ):
if summ > n or len(sol) > k:
return
if summ == n and len(sol) == k:
ans.append(sol.copy())
return
for i in range(start, 10):
sol.append(i)
dfs(i+1, i + summ)
sol.pop()
sol = []
ans = []
dfs(1,0)
return ans