forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationsII.java
More file actions
34 lines (33 loc) · 1.23 KB
/
PermutationsII.java
File metadata and controls
34 lines (33 loc) · 1.23 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
public class Solution {
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> results
= new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> result
= new ArrayList<Integer>();
boolean[] set = new boolean[num.length];
Arrays.sort(num);
permuteUnique(results, result, set, num, 0);
return results;
}
public void permuteUnique(ArrayList<ArrayList<Integer>> results,
ArrayList<Integer> result, boolean[] set, int[] num, int count){
if(count == num.length){
results.add(new ArrayList<Integer>(result));
}
else{
for(int i = 0; i < num.length; ++i){
if(!set[i]){
set[i] = true;
result.add(num[i]);
permuteUnique(results, result, set, num, count + 1);
result.remove(result.size() - 1);
set[i] = false;
while(i < num.length - 1 && num[i] == num[i + 1])
++i;
}
}
}
}
}