-
Notifications
You must be signed in to change notification settings - Fork 29
/
Solution40.java
42 lines (33 loc) · 1.39 KB
/
Solution40.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
package backstracking_problem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution40 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> combinations = new ArrayList<>();
List<Integer> tempCombination = new ArrayList<>();
boolean[] visited = new boolean[candidates.length];
Arrays.sort(candidates);
backtracking(tempCombination, combinations, visited, 0, target, candidates);
return combinations;
}
private void backtracking(List<Integer> tempCombination, List<List<Integer>> combinations, boolean[] visited, int start, int target, int[] candidates) {
if (target == 0) {
combinations.add(new ArrayList<>(tempCombination));
return;
}
for (int i = start; i < candidates.length; i++) {
// 去重处理
if (i != 0 && candidates[i] == candidates[i - 1] && !visited[i - 1]) {
continue;
}
if (candidates[i] <= target) {
visited[i] = true;
tempCombination.add(candidates[i]);
backtracking(tempCombination, combinations, visited, i + 1, target - candidates[i], candidates);
tempCombination.remove(tempCombination.size() - 1);
visited[i] = false;
}
}
}
}