Leetcode 39.组合总和

39.组合总和

题目

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

所有数字(包括 target)都是正整数。
解集不能包含重复的组合。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

方法

方法1:回溯法

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res=[]
self.backtrack(candidates,target,res,[],0)
return res

def backtrack(self,candidates,target,res,temp,start):
if target<0:
return
elif target==0:
res.append(list(temp))
else:
for i in range(start,len(candidates)):
temp.append(candidates[i])
self.backtrack(candidates,target-candidates[i],res,temp,i) # 元素可以重复使用,所以此处是i,不是i+1
temp.pop()
# backtrack([2, 3, 6, 7], 7, [], [], 0)
# backtrack([2, 3, 6, 7], 5, [], [2], 0)
# backtrack([2, 3, 6, 7], 3, [], [2, 2], 0)
# backtrack([2, 3, 6, 7], 1, [], [2, 2, 2], 0)
# backtrack([2, 3, 6, 7], -1, [], [2, 2, 2, 2], 0)
# pop: 2
# --------------------
# backtrack([2, 3, 6, 7], -2, [], [2, 2, 2, 3], 1)
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], -5, [], [2, 2, 2, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -6, [], [2, 2, 2, 7], 3)
# pop: 7
# --------------------
# pop: 2
# --------------------
# backtrack([2, 3, 6, 7], 0, [], [2, 2, 3], 1)
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], -3, [[2, 2, 3]], [2, 2, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -4, [[2, 2, 3]], [2, 2, 7], 3)
# pop: 7
# --------------------
# pop: 2
# --------------------
# backtrack([2, 3, 6, 7], 2, [[2, 2, 3]], [2, 3], 1)
# backtrack([2, 3, 6, 7], -1, [[2, 2, 3]], [2, 3, 3], 1)
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], -4, [[2, 2, 3]], [2, 3, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -5, [[2, 2, 3]], [2, 3, 7], 3)
# pop: 7
# --------------------
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], -1, [[2, 2, 3]], [2, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -2, [[2, 2, 3]], [2, 7], 3)
# pop: 7
# --------------------
# pop: 2
# --------------------
# backtrack([2, 3, 6, 7], 4, [[2, 2, 3]], [3], 1)
# backtrack([2, 3, 6, 7], 1, [[2, 2, 3]], [3, 3], 1)
# backtrack([2, 3, 6, 7], -2, [[2, 2, 3]], [3, 3, 3], 1)
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], -5, [[2, 2, 3]], [3, 3, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -6, [[2, 2, 3]], [3, 3, 7], 3)
# pop: 7
# --------------------
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], -2, [[2, 2, 3]], [3, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -3, [[2, 2, 3]], [3, 7], 3)
# pop: 7
# --------------------
# pop: 3
# --------------------
# backtrack([2, 3, 6, 7], 1, [[2, 2, 3]], [6], 2)
# backtrack([2, 3, 6, 7], -5, [[2, 2, 3]], [6, 6], 2)
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], -6, [[2, 2, 3]], [6, 7], 3)
# pop: 7
# --------------------
# pop: 6
# --------------------
# backtrack([2, 3, 6, 7], 0, [[2, 2, 3]], [7], 3)
# pop: 7
# --------------------
# [[2, 2, 3], [7]]