-
Notifications
You must be signed in to change notification settings - Fork 232
/
CombinationSum2_1203.java
executable file
·68 lines (59 loc) · 2.17 KB
/
CombinationSum2_1203.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
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
package Algorithms.combination;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CombinationSum2_1203 {
public List<List<Integer>> combinationSum2(int[] num, int target) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (num == null || num.length == 0) {
return ret;
}
Arrays.sort(num);
dfs(num, target, new ArrayList<Integer>(), ret, 0);
return ret;
}
public void dfs1(int[] num, int target, ArrayList<Integer> path, List<List<Integer>> ret, int index) {
if (target == 0) {
ret.add(new ArrayList<Integer>(path));
return;
}
if (target < 0) {
return;
}
// 注意,这里从 i = index开始
// 每次只取第一个,例如 123334,到了333这里,我们第一次只取第1个3,因为我们选任何一个3是对组合来说是一个解。所以只
// 第一次取就好了。
int pre = -1;
for (int i = index; i < num.length; i++) {
int n = num[i];
if (n == pre) {
continue;
}
pre = n;
path.add(n);
dfs(num, target - n, path, ret, i + 1);
path.remove(path.size() - 1);
}
}
public void dfs(int[] num, int target, ArrayList<Integer> path, List<List<Integer>> ret, int index) {
if (target == 0) {
ret.add(new ArrayList<Integer>(path));
return;
}
if (target < 0) {
return;
}
// 注意,这里从 i = index开始
// 每次只取第一个,例如 123334,到了333这里,我们第一次只取第1个3,因为我们选任何一个3是对组合来说是一个解。所以只
// 第一次取就好了。
for (int i = index; i < num.length; i++) {
int n = num[i];
if (i != index && n == num[i - 1]) {
continue;
}
path.add(n);
dfs(num, target - n, path, ret, i + 1);
path.remove(path.size() - 1);
}
}
}