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

Commitcb7d5f4

Browse files
permutations II
1 parentc9d93f6 commitcb7d5f4

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

‎MEDIUM/src/medium/PermutationsII.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packagemedium;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.List;
6+
7+
importutils.CommonUtils;
8+
9+
publicclassPermutationsII {
10+
/**Looked at this post: https://discuss.leetcode.com/topic/31445/really-easy-java-solution-much-easier-than-the-solutions-with-very-high-vote*/
11+
publicList<List<Integer>>permuteUnique(int[]nums) {
12+
List<List<Integer>>result =newArrayList();
13+
if (nums ==null ||nums.length ==0)returnresult;
14+
boolean[]used =newboolean[nums.length];
15+
List<Integer>list =newArrayList();
16+
Arrays.sort(nums);
17+
dfs(nums,used,list,result);
18+
returnresult;
19+
}
20+
21+
22+
privatevoiddfs(int[]nums,boolean[]used,List<Integer>list,List<List<Integer>>result) {
23+
if (list.size() ==nums.length){
24+
result.add(newArrayList(list));
25+
return;
26+
}
27+
for (inti =0;i <nums.length;i++){
28+
if (used[i])continue;
29+
if (i >0 &&nums[i -1] ==nums[i] && !used[i -1])
30+
continue;
31+
/**
32+
* For this line, both !used[i-1] and used[i-1] will AC. It is because the first one makes sure when
33+
* duplicates are selected, the order is ascending (index from small to large). However,
34+
* the second one means the descending order.
35+
*/
36+
used[i] =true;
37+
list.add(nums[i]);
38+
dfs(nums,used,list,result);
39+
used[i] =false;
40+
list.remove(list.size()-1);
41+
}
42+
}
43+
44+
45+
publicstaticvoidmain(String...args){
46+
int[]nums =newint[]{1,1,2};
47+
PermutationsIItest =newPermutationsII();
48+
List<List<Integer>>result =test.permuteUnique(nums);
49+
CommonUtils.printIntegerList(result);
50+
}
51+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
|67|[Add Binary](https://leetcode.com/problems/add-binary/)|[Solution](../../blob/master/EASY/src/easy/AddBinary.java)|O(n)|O(1)|Easy|
116116
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../../blob/master/EASY/src/easy/LengthofLastWord.java)|O(n)|O(1)|Easy|
117117
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../../blob/master/HARD/src/hard/MergeIntervals.java)|O(n*logn)|O(1)|Hard|
118+
|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)|[Solution](../../blob/master/MEDIUM/src/medium/PermutationsII.java)|O(n*n!)|O(n)|Medium|Backtracking
118119
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution]|||Medium
119120
|31|[Next Permutation](https://leetcode.com/problems/next-permutation)|[Solution](../../blob/master/MEDIUM/src/medium/NextPermutation.java)|O(n)|O(1)|Medium|Array
120121
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)|[Solution](../../blob/master/EASY/src/easy/SwapNodesinPairs.java)|O(n)|O(1)|Easy| Recursion, LinkedList

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp