-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLeetCode40.java
More file actions
69 lines (64 loc) · 2.3 KB
/
LeetCode40.java
File metadata and controls
69 lines (64 loc) · 2.3 KB
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
package problems;
import java.util.*;
public class LeetCode40 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Deque<Integer> path = new ArrayDeque<>();
int length = candidates.length;
if(length == 0){
return res;
}
// 关键步骤
Arrays.sort(candidates);
backtrack(candidates, 0, length, target, path, res);
return res;
}
private void backtrack(int[] candidates, int begin, int length, int target, Deque<Integer> path, List<List<Integer>> res) {
if(target == 0){
res.add(new ArrayList<>(path));
return;
}
for (int i = begin; i < length; i++) {
// 大剪枝:减去candidate[i] 小于0,减去后面的 candidate[i+1]、candidate[i+2]肯定也小于0,因此用 break;
if(target - candidates[i] < 0){
break;
}
// 小剪枝:同一层相同数值的结点,从第2个开始,候选数更少,因此跳过,用continue
if(i > begin && candidates[i] == candidates[i-1]){
continue;
}
path.add(candidates[i]);
backtrack(candidates, i+1, length, target - candidates[i], path, res);
path.removeLast();
}
}
}
class LeetCode40_1 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans = new ArrayList<>();
Deque<Integer> res = new ArrayDeque<>();
dfs(candidates, ans, res, 0, target);
return ans;
}
private void dfs(int[] candidates, List<List<Integer>> ans, Deque<Integer> res, int index, int target) {
if(target < 0) {
return;
}
if(target == 0) {
ans.add(new ArrayList<>(res));
return;
}
for (int i = index; i < candidates.length; i++) {
if(target - candidates[i] < 0) {
break;
}
if(i > index && candidates[i] == candidates[i - 1]) {
continue;
}
res.add(candidates[i]);
dfs(candidates, ans, res, i + 1, target - candidates[i]);
res.removeLast();
}
}
}