-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.py
35 lines (29 loc) · 1.2 KB
/
Solution.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
from typing import List
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
result = []
def dfs(searchList: List[int], path: List[int], target: int, result: List[List[int]]):
if target == 0:
result.append(path)
return
elif target < 0:
return
for i in range(len(searchList)):
dfs(searchList[i:], path + [searchList[i]], target - searchList[i], result)
dfs(candidates, [], target, result)
return result
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
result = []
def dfs(searchList: List[int], path: List[int], target: int, start: int, result: List[List[int]]):
if target == 0:
result.append(path[:])
return
elif target < 0:
return
for i in range(start, len(searchList)):
path.append(searchList[i])
dfs(searchList, path, target - searchList[i], start, result)
path.pop()
dfs(candidates, [], target, 0, result)
return result