Skip to content

Commit a962eed

Browse files
add a solution for 47
1 parent f056ba0 commit a962eed

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/main/java/com/fishercoder/solutions/_47.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.HashSet;
56
import java.util.List;
7+
import java.util.Set;
68

79
public class _47 {
810
public static class Solution1 {
@@ -15,14 +17,13 @@ public List<List<Integer>> permuteUnique(int[] nums) {
1517
return result;
1618
}
1719
boolean[] used = new boolean[nums.length];
18-
List<Integer> list = new ArrayList();
19-
Arrays.sort(nums);
20-
dfs(nums, used, list, result);
20+
Arrays.sort(nums);//this sorting is critical for the correctness of this backtracking algorithm as we compare the two adjancent neighbors to filter out possible duplicate permutations
21+
backtracking(nums, used, new ArrayList(), result);
2122
return result;
2223
}
2324

2425

25-
private void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> result) {
26+
private void backtracking(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> result) {
2627
if (list.size() == nums.length) {
2728
result.add(new ArrayList(list));
2829
return;
@@ -31,20 +32,51 @@ private void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integ
3132
if (used[i]) {
3233
continue;
3334
}
34-
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) {
35+
if (i > 0 && nums[i - 1] == nums[i] && used[i - 1]) {
36+
/**
37+
* For this line, both !used[i-1] and used[i-1] will AC.
38+
* It is because the first one makes sure when duplicates are selected, the order is ascending (index from small to large).
39+
* However, the second one means the descending order.
40+
* 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.
41+
*/
3542
continue;
3643
}
37-
/**
38-
* For this line, both !used[i-1] and used[i-1] will AC. It is because the first one makes sure when
39-
* duplicates are selected, the order is ascending (index from small to large). However,
40-
* the second one means the descending order.
41-
*/
4244
used[i] = true;
4345
list.add(nums[i]);
44-
dfs(nums, used, list, result);
46+
backtracking(nums, used, list, result);
4547
used[i] = false;
4648
list.remove(list.size() - 1);
4749
}
4850
}
4951
}
52+
53+
public static class Solution2 {
54+
public List<List<Integer>> permuteUnique(int[] nums) {
55+
Arrays.sort(nums);
56+
Set<List<Integer>> set = new HashSet<>();
57+
set.add(new ArrayList<>());
58+
set = recursion(nums, set, 0);
59+
List<List<Integer>> res = new ArrayList<>();
60+
for (List<Integer> list : set) {
61+
res.add(list);
62+
}
63+
return res;
64+
}
65+
66+
private Set<List<Integer>> recursion(int[] nums, Set<List<Integer>> set, int pos) {
67+
if (pos == nums.length) {
68+
return set;
69+
}
70+
Set<List<Integer>> newSet = new HashSet<>();
71+
for (List<Integer> list : set) {
72+
for (int i = 0; i <= list.size(); i++) {
73+
List<Integer> newList = new ArrayList<>(list);
74+
newList.add(i, nums[pos]);
75+
newSet.add(newList);
76+
}
77+
}
78+
set = newSet;
79+
return recursion(nums, set, pos + 1);
80+
}
81+
}
5082
}

src/test/java/com/fishercoder/_47Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99

1010
public class _47Test {
1111
private static _47.Solution1 solution1;
12+
private static _47.Solution2 solution2;
1213
private static List<List<Integer>> actual;
1314

1415
@BeforeClass
1516
public static void setup() {
1617
solution1 = new _47.Solution1();
18+
solution2 = new _47.Solution2();
1719
}
1820

1921
@Test
2022
public void test1() {
2123
actual = solution1.permuteUnique(new int[]{1, 1, 2});
2224
CommonUtils.printListList(actual);
25+
actual = solution2.permuteUnique(new int[]{1, 1, 2});
26+
CommonUtils.printListList(actual);
2327
}
2428

2529
}

0 commit comments

Comments
 (0)