-
Notifications
You must be signed in to change notification settings - Fork 0
/
47. 全排列 II.java
59 lines (50 loc) · 1.91 KB
/
47. 全排列 II.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
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> list = new LinkedList<>();
Arrays.sort(nums);
dfs(nums, list, new LinkedList<>(), new boolean[nums.length], 0);
return list;
}
private void dfs(int[] nums, List<List<Integer>> list, List<Integer> path, boolean[] used, int depth){
if(depth == nums.length){
list.add(new LinkedList<>(path));
return;
}
for(int i = 0; i < nums.length; i++){
if(used[i] || (i > 0 && nums[i] == nums[i - 1] && used[i - 1])){ // 后半部分是去重
continue;
}
path.add(nums[i]);
used[i] = true;
dfs(nums, list, path, used, depth + 1);
used[i] = false;
path.remove(path.size() - 1);
}
}
}
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
Set<List<Integer>> res = new HashSet<>();
if(nums == null || nums.length == 0){
return new LinkedList<List<Integer>>(res);
}
permuteDFS(nums, res, new boolean[nums.length], 0, new LinkedList<Integer>());
return new LinkedList<List<Integer>>(res);
}
private void permuteDFS(int[] nums, Set<List<Integer>> res, boolean[] used, int depth, LinkedList<Integer> path){
if(depth == nums.length){
// res.add((LinkedList<Integer>)path.clone()); // 因为向res中添加的path是引用类型,该path一直会变,因此虽然添加的时候是对的,但是之后会被改变。
res.add(new LinkedList(path));
}
for(int i = 0; i < nums.length; i++){
if(used[i]){
continue;
}
path.add(nums[i]);
used[i] = true;
permuteDFS(nums, res, used, depth + 1, path);
path.pollLast();
used[i] = false;
}
}
}