-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation2.java
More file actions
28 lines (28 loc) · 924 Bytes
/
Permutation2.java
File metadata and controls
28 lines (28 loc) · 924 Bytes
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
public class Permutation2 {
ArrayList<ArrayList<Integer>> ans;
boolean[] vsd;
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
Arrays.sort(num);
ans = new ArrayList<ArrayList<Integer>>();
vsd = new boolean[num.length];
solve(num, 0, new ArrayList<Integer>());
return ans;
}
void solve(int[] num, int from, ArrayList<Integer> tmp){
if(from == num.length){
ans.add(new ArrayList<Integer>(tmp));
}else{
int last = Integer.MIN_VALUE;
for(int i = 0; i < num.length; i++){
if(!vsd[i] && num[i] != last){
last = num[i];
vsd[i] = true;
tmp.add(num[i]);
solve(num, from+1,tmp);
vsd[i] = false;
tmp.remove(tmp.size() -1);
}
}
}
}
}