-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ90SubsetsII.java
More file actions
47 lines (43 loc) · 1.11 KB
/
Q90SubsetsII.java
File metadata and controls
47 lines (43 loc) · 1.11 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 90.SubsetsII
*
* @author ahscuml
* @date 2018/9/13
* @time 20:50
*/
public class Q90SubsetsII {
/**
* 测试函数
*/
public static void main(String[] args) {
int[] nums = {1, 2, 2};
System.out.println(subsetWithDup(nums).toString());
}
/**
*
* */
public static List<List<Integer>> subsetWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
backTracking(list, new ArrayList<>(), nums, 0);
return list;
}
/**
*
* */
public static void backTracking(List<List<Integer>> list, List<Integer> templist, int[] nums, int start) {
list.add(new ArrayList<>(templist));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
} else {
templist.add(nums[i]);
backTracking(list, templist, nums, i + 1);
templist.remove(templist.size() - 1);
}
}
}
}