-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllUniquePermutationsOfAnArray.java
41 lines (35 loc) · 1.26 KB
/
AllUniquePermutationsOfAnArray.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
//User function Template for Java
class Solution {
static ArrayList<ArrayList<Integer>> uniquePerms(ArrayList<Integer> arr , int n) {
// code here
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
HashSet<ArrayList<Integer>> set = new HashSet<>();
generatePermutations(arr, n, 0, set);
for (ArrayList<Integer> perm : set) {
result.add(perm);
}
Collections.sort(result, new Comparator<ArrayList<Integer>>() {
@Override
public int compare(ArrayList<Integer> a, ArrayList<Integer> b) {
for (int i = 0; i < n; i++) {
if (a.get(i) != b.get(i)) {
return a.get(i) - b.get(i);
}
}
return 0;
}
});
return result;
}
static void generatePermutations(ArrayList<Integer> arr, int n, int index, HashSet<ArrayList<Integer>> set) {
if (index == n) {
set.add(new ArrayList<>(arr));
return;
}
for (int i = index; i < n; i++) {
Collections.swap(arr, index, i);
generatePermutations(arr, n, index + 1, set);
Collections.swap(arr, index, i);
}
}
}