-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path39.py
41 lines (32 loc) · 985 Bytes
/
39.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
from typing import List
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
"""
Parameters
----------
candidates : List[int]
输入的无重复的数组
target : int
求和的目标值
Returns
-------
List[List[int]]
结果的组合
"""
res = []
temp = []
n = len(candidates)
def dfs(_target: int, idx:int):
if _target < 0:
return # 剪枝
elif _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)
temp.pop()
dfs(target, 0)
return res