Skip to content

Commit fc70143

Browse files
refactor 47
1 parent a307614 commit fc70143

File tree

2 files changed

+63
-40
lines changed

2 files changed

+63
-40
lines changed
Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
import java.util.ArrayList;
64
import java.util.Arrays;
75
import java.util.List;
86

9-
/**Given a collection of numbers that might contain duplicates, return all possible unique permutations.
7+
/**
8+
* 47. Permutations II
9+
*
10+
* Given a collection of numbers that might contain duplicates, return all possible unique permutations.
1011
1112
For example,
1213
[1,1,2] have the following unique permutations:
@@ -16,49 +17,46 @@
1617
[2,1,1]
1718
]*/
1819
public class _47 {
19-
/**credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote*/
20-
public List<List<Integer>> permuteUnique(int[] nums) {
21-
List<List<Integer>> result = new ArrayList();
22-
if (nums == null || nums.length == 0) {
20+
public static class Solution1 {
21+
/**
22+
* credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote
23+
*/
24+
public List<List<Integer>> permuteUnique(int[] nums) {
25+
List<List<Integer>> result = new ArrayList();
26+
if (nums == null || nums.length == 0) {
27+
return result;
28+
}
29+
boolean[] used = new boolean[nums.length];
30+
List<Integer> list = new ArrayList();
31+
Arrays.sort(nums);
32+
dfs(nums, used, list, result);
2333
return result;
2434
}
25-
boolean[] used = new boolean[nums.length];
26-
List<Integer> list = new ArrayList();
27-
Arrays.sort(nums);
28-
dfs(nums, used, list, result);
29-
return result;
30-
}
3135

3236

33-
private void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> result) {
34-
if (list.size() == nums.length) {
35-
result.add(new ArrayList(list));
36-
return;
37-
}
38-
for (int i = 0; i < nums.length; i++) {
39-
if (used[i]) {
40-
continue;
37+
private void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> result) {
38+
if (list.size() == nums.length) {
39+
result.add(new ArrayList(list));
40+
return;
4141
}
42-
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) {
43-
continue;
42+
for (int i = 0; i < nums.length; i++) {
43+
if (used[i]) {
44+
continue;
45+
}
46+
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) {
47+
continue;
48+
}
49+
/**
50+
* For this line, both !used[i-1] and used[i-1] will AC. It is because the first one makes sure when
51+
* duplicates are selected, the order is ascending (index from small to large). However,
52+
* the second one means the descending order.
53+
*/
54+
used[i] = true;
55+
list.add(nums[i]);
56+
dfs(nums, used, list, result);
57+
used[i] = false;
58+
list.remove(list.size() - 1);
4459
}
45-
/**
46-
* For this line, both !used[i-1] and used[i-1] will AC. It is because the first one makes sure when
47-
* duplicates are selected, the order is ascending (index from small to large). However,
48-
* the second one means the descending order.
49-
*/
50-
used[i] = true;
51-
list.add(nums[i]);
52-
dfs(nums, used, list, result);
53-
used[i] = false;
54-
list.remove(list.size() - 1);
5560
}
5661
}
57-
58-
public static void main(String... args) {
59-
int[] nums = new int[]{1, 1, 2};
60-
_47 test = new _47();
61-
List<List<Integer>> result = test.permuteUnique(nums);
62-
CommonUtils.printListList(result);
63-
}
6462
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._47;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
public class _47Test {
11+
private static _47.Solution1 solution1;
12+
private static List<List<Integer>> actual;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _47.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
actual = solution1.permuteUnique(new int[]{1, 1, 2});
22+
CommonUtils.printListList(actual);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy