-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution39.java
43 lines (36 loc) · 1.13 KB
/
Solution39.java
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
package leetcode.backtrack;
import java.util.LinkedList;
import java.util.List;
public class Solution39 {
List<List<Integer>> res;
int target;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
res = new LinkedList<>();
this.target = target;
backtrack(candidates, 0, new LinkedList<>(), 0);
return res;
}
/**
* 1. 当前问题:向路径中添加第 i 个元素
* 2. 每一步的操作:直接添加元素
* 3. 子问题:向路径中添加第 i + 1 个元素
* 4. 剪枝优化:sum > target
*/
private void backtrack(int[] candidates, int index, LinkedList<Integer> path, int sum) {
if (sum > target) {
return;
}
if (sum == target) {
res.add((List<Integer>) path.clone());
return;
}
for (int i = index; i < candidates.length; i++) {
path.add(candidates[i]);
sum += candidates[i];
backtrack(candidates, i, path, sum);
// 恢复现场
path.removeLast();
sum -= candidates[i];
}
}
}