-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path040-combination-sum-ii.py
63 lines (49 loc) · 1.49 KB
/
040-combination-sum-ii.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
"""
from typing import List
def combination_sum2(candidates: List[int], target: int) -> List[List[int]]:
answers = []
candidates_len = len(candidates)
candidates.sort()
def solve(expected: int, start: int, ans: List[int]):
if expected < 0:
return
elif expected == 0:
if ans not in answers:
answers.append(ans.copy())
else:
for i in range(start, candidates_len):
ans.append(candidates[i])
solve(expected - candidates[i], i + 1, ans)
ans.pop()
# del(ans[-1])
if candidates_len > 0:
answer = []
solve(target, 0, answer)
return answers
if __name__ == '__main__':
print(combination_sum2([10, 1, 2, 7, 6, 1, 5], 8))
print(combination_sum2([2, 5, 2, 1, 2], 5))
print(combination_sum2([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 27))