-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path216.py
More file actions
26 lines (21 loc) · 742 Bytes
/
Copy path216.py
File metadata and controls
26 lines (21 loc) · 742 Bytes
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
from typing import List
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
temp = []
candidates = list(range(1, 10))
target = n
n = len(candidates)
def dfs(_target: int, idx:int, depth:int):
if depth == k:
if _target == 0:
res.append(temp[:])
return
for idx in range(idx, n):
val = candidates[idx]
if val <= _target:
temp.append(val)
dfs(_target - val, idx+1, depth+1)
temp.pop()
dfs(target, 0, 0)
return res