Skip to content

Commit cb7d5f4

Browse files
permutations II
1 parent c9d93f6 commit cb7d5f4

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+
package medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import utils.CommonUtils;
8+
9+
public class PermutationsII {
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+
public List<List<Integer>> permuteUnique(int[] nums) {
12+
List<List<Integer>> result = new ArrayList();
13+
if (nums == null || nums.length == 0) return result;
14+
boolean[] used = new boolean[nums.length];
15+
List<Integer> list = new ArrayList();
16+
Arrays.sort(nums);
17+
dfs(nums, used, list, result);
18+
return result;
19+
}
20+
21+
22+
private void dfs(int[] nums, boolean[] used, List<Integer> list, List<List<Integer>> result) {
23+
if (list.size() == nums.length){
24+
result.add(new ArrayList(list));
25+
return;
26+
}
27+
for (int i = 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+
public static void main(String...args){
46+
int[] nums = new int[]{1,1,2};
47+
PermutationsII test = new PermutationsII();
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)
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