Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc70143

Browse files
refactor 47
1 parenta307614 commitfc70143

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
packagecom.fishercoder.solutions;
22

3-
importcom.fishercoder.common.utils.CommonUtils;
4-
53
importjava.util.ArrayList;
64
importjava.util.Arrays;
75
importjava.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
publicclass_47 {
19-
/**credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote*/
20-
publicList<List<Integer>>permuteUnique(int[]nums) {
21-
List<List<Integer>>result =newArrayList();
22-
if (nums ==null ||nums.length ==0) {
20+
publicstaticclassSolution1 {
21+
/**
22+
* credit: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote
23+
*/
24+
publicList<List<Integer>>permuteUnique(int[]nums) {
25+
List<List<Integer>>result =newArrayList();
26+
if (nums ==null ||nums.length ==0) {
27+
returnresult;
28+
}
29+
boolean[]used =newboolean[nums.length];
30+
List<Integer>list =newArrayList();
31+
Arrays.sort(nums);
32+
dfs(nums,used,list,result);
2333
returnresult;
2434
}
25-
boolean[]used =newboolean[nums.length];
26-
List<Integer>list =newArrayList();
27-
Arrays.sort(nums);
28-
dfs(nums,used,list,result);
29-
returnresult;
30-
}
3135

3236

33-
privatevoiddfs(int[]nums,boolean[]used,List<Integer>list,List<List<Integer>>result) {
34-
if (list.size() ==nums.length) {
35-
result.add(newArrayList(list));
36-
return;
37-
}
38-
for (inti =0;i <nums.length;i++) {
39-
if (used[i]) {
40-
continue;
37+
privatevoiddfs(int[]nums,boolean[]used,List<Integer>list,List<List<Integer>>result) {
38+
if (list.size() ==nums.length) {
39+
result.add(newArrayList(list));
40+
return;
4141
}
42-
if (i >0 &&nums[i -1] ==nums[i] && !used[i -1]) {
43-
continue;
42+
for (inti =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-
publicstaticvoidmain(String...args) {
59-
int[]nums =newint[]{1,1,2};
60-
_47test =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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._47;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importjava.util.List;
9+
10+
publicclass_47Test {
11+
privatestatic_47.Solution1solution1;
12+
privatestaticList<List<Integer>>actual;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_47.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
actual =solution1.permuteUnique(newint[]{1,1,2});
22+
CommonUtils.printListList(actual);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp