forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0047Permutation2.java
More file actions
35 lines (33 loc) · 1.17 KB
/
Copy pathP0047Permutation2.java
File metadata and controls
35 lines (33 loc) · 1.17 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class P0047Permutation2 {
List<List<Integer>> ans;
public List<List<Integer>> permuteUnique(int[] nums) {
ans = new ArrayList<>();
Arrays.sort(nums);
boolean[]used = new boolean[nums.length];
backtrack(new ArrayList<>(),nums,used);
return ans;
}
public void backtrack(List<Integer>tempList,int[]nums,boolean[]used){
if(tempList.size()==nums.length){
ans.add(new ArrayList<>(tempList));
return;
}
for(int i = 0;i<nums.length;i++){
if(used[i])continue;
if(i>0 && nums[i]==nums[i-1] && !used[i-1]) continue;
//如果没有!used[i-1]这个条件则答案是空集合。因为如果有重复元素则总是used[i]=true,所以无法添加结果集合为array同等的size
tempList.add(nums[i]);
used[i]=true;
backtrack(tempList,nums,used);
used[i]=false;
tempList.remove(tempList.size()-1);
}
}
}
/*
P0047Permutation2 p47 = new P0047Permutation2();
System.out.println(p47.permuteUnique(new int[]{1,1,2}));
*/