forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_47.java
80 lines (74 loc) · 3.09 KB
/
_47.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class _47 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote
*/
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> result = new ArrayList();
if (nums == null || nums.length == 0) {
return result;
}
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);//this sorting is critical for the correctness of this backtracking algorithm as we compare the two adjacent neighbors to filter out possible duplicate permutations
backtracking(nums, used, new ArrayList(), result);
return result;
}
private void backtracking(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> result) {
if (list.size() == nums.length) {
result.add(new ArrayList(list));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) {
continue;
}
if (i > 0 && nums[i - 1] == nums[i] && used[i - 1]) {
/**
* For this line, both !used[i-1] and used[i-1] will AC.
* It is because the first one makes sure when duplicates are selected, the order is ascending (index from small to large).
* However, the second one means the descending order.
* But without this used[i - 1] or !used[i - 1] will not yield a correct result as the program will not yield a correct result.
*/
continue;
}
used[i] = true;
list.add(nums[i]);
backtracking(nums, used, list, result);
used[i] = false;
list.remove(list.size() - 1);
}
}
}
public static class Solution2 {
public List<List<Integer>> permuteUnique(int[] nums) {
Set<List<Integer>> set = new HashSet<>();
set.add(new ArrayList<>());
set = recurse(nums, set, 0);
List<List<Integer>> res = new ArrayList<>();
for (List<Integer> list : set) {
res.add(list);
}
return res;
}
private Set<List<Integer>> recurse(int[] nums, Set<List<Integer>> set, int pos) {
if (pos == nums.length) {
return set;
}
Set<List<Integer>> newSet = new HashSet<>();
for (List<Integer> list : set) {
for (int i = 0; i <= list.size(); i++) {
List<Integer> newList = new ArrayList<>(list);
newList.add(i, nums[pos]);
newSet.add(newList);
}
}
return recurse(nums, newSet, pos + 1);
}
}
}