forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation2.java
More file actions
executable file
·40 lines (37 loc) · 1.13 KB
/
permutation2.java
File metadata and controls
executable file
·40 lines (37 loc) · 1.13 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
public class Solution {
boolean used[];
int res[];
int num[];
HashSet<ArrayList<Integer>> resSet;
ArrayList<ArrayList<Integer>> reslist;
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
// Start typing your Java solution below
// DO NOT rite main() function
used = new boolean[num.length];
res = new int[num.length];
this.num = num;
reslist = new ArrayList<ArrayList<Integer>>();
resSet = new HashSet<ArrayList<Integer>>();
pu(0);
reslist.addAll(resSet);
return reslist;
}
public void pu(int x){
for(int i=0;i<num.length;i++){
if(used[i]==false){
used[i] = true;
res[x] = num[i];
if(x==num.length-1){
ArrayList<Integer> y = new ArrayList<Integer>();
for(int j=0;j<res.length;j++){
y.add(res[j]);
}
resSet.add(y);
}else{
pu(x+1);
}
used[i] = false;
}
}
}
}