super easy dfs
This commit is contained in:
parent
045676e191
commit
e95027872a
16
39-240619-pass/main.py
Normal file
16
39-240619-pass/main.py
Normal file
@ -0,0 +1,16 @@
|
||||
class Solution:
|
||||
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:
|
||||
candidates.sort()
|
||||
rlt = []
|
||||
def dfs(cur, l, cur_num):
|
||||
if cur == 0:
|
||||
rlt.append(l)
|
||||
for candidate in candidates:
|
||||
if cur - candidate >= 0 and candidate >= cur_num:
|
||||
dfs(cur - candidate, l + [candidate], candidate)
|
||||
dfs(target, [], candidates[0])
|
||||
return rlt
|
||||
|
||||
|
||||
sol = Solution()
|
||||
print(sol.combinationSum([2,3,6,7], 7))
|
Loading…
Reference in New Issue
Block a user