-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
39 lines (31 loc) · 829 Bytes
/
test.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
# -*- coding: utf-8 -*-
'''
@Time : 2023/7/26 11:57
@Author : cooper
@File : test.py
'''
def combinationSum(candidates, target):
n = len(candidates)
path = []
res = []
if not candidates:
return []
def backtrack(begin, path, res, target):
print(f'target:{target}')
if target < 0:
return
elif target == 0:
print(f'mid:{path}')
res.append(path)
return
else:
print(f'before:{path}')
for index in range(begin, n):
backtrack(index, path + [candidates[index]], res, target - candidates[index])
print(f'after:{path}')
backtrack(0, path, res, target)
return res
candidates = [2,3,6,7]
target = 7
ans = combinationSum(candidates, target)
print('ans:',ans)