Skip to content

Commit ebaec03

Browse files
committed
047 (3) cleanup solution
1 parent 8848c5a commit ebaec03

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/_047_PermutationsII/Solution.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* Description:
88
*
99
* Given a collection of numbers that might contain duplicates, return all
10-
* possible unique permutations.
10+
* possible unique subs.
1111
*
1212
* For example,
13-
* [1,1,2] have the following unique permutations:
13+
* [1,1,2] have the following unique subs:
1414
* [1,1,2], [1,2,1], and [2,1,1].
1515
*
1616
***************************************************************************
17-
* {@link https://leetcode.com/problems/permutations-ii/ }
17+
* {@link https://leetcode.com/problems/subs-ii/ }
1818
* P.S.: cannot skip duplicates using while (nums[i] == duplicate) because
1919
* after swapping, duplicates may be separated (one duplicate is swapped with
2020
* and goes to somewhere else disconnected with other duplicates)
@@ -37,44 +37,44 @@ public List<List<Integer>> permuteUnique(int[] nums) {
3737
}
3838
Arrays.sort(nums);
3939
int index = 0;
40-
List<Integer> permutation = new ArrayList<Integer>();
41-
permuteUnique(nums, index, permutation, result);
40+
List<Integer> sub = new ArrayList<Integer>();
41+
permuteUnique(nums, index, sub, result);
4242
return result;
4343
}
4444

45-
private void permuteUnique(int[] nums, int index,
46-
List<Integer> permutation, List<List<Integer>> result) {
47-
45+
private void permuteUnique(int[] nums, int index, List<Integer> sub,
46+
List<List<Integer>> result) {
4847
// base case
4948
if (index == nums.length) {
50-
// one valid permutation found
51-
result.add(permutation);
49+
// one valid sub found
50+
result.add(sub);
5251
return;
5352
}
5453

5554
// recursive case
5655
// either use set or sort nums[index]:nums[end] to avoid duplicates
5756
Set<Integer> appearred = new HashSet<Integer>();
5857
for (int i = index; i < nums.length; i++) {
59-
// try each number among nums[index],...,nums[end]
60-
// as permutation[index], remember to skip duplicates!
61-
if (appearred.contains(nums[i]) == false) {
62-
// swap nums[i] with nums[index]
63-
swap(nums, i, index);
58+
if (appearred.contains(nums[i])) {
59+
// duplicates appear
60+
continue;
61+
}
62+
appearred.add(nums[i]);
63+
// swap nums[i] with nums[index]
64+
swap(nums, i, index);
6465

65-
// go on searching
66-
List<Integer> copy = new ArrayList<Integer>(permutation);
67-
copy.add(nums[index]);
68-
permuteUnique(nums, index + 1, copy, result);
66+
// go on searching
67+
List<Integer> copy = new ArrayList<Integer>(sub);
68+
copy.add(nums[index]);
69+
permuteUnique(nums, index + 1, copy, result);
6970

70-
// swap back nums[i] with nums[index]
71-
swap(nums, index, i);
71+
// swap back nums[i] with nums[index]
72+
swap(nums, index, i);
7273

73-
appearred.add(nums[i]);
74-
}
7574
}
7675
}
7776

77+
// swap two numbers in an array
7878
private void swap(int[] nums, int i, int index) {
7979
if (i < nums.length && index < nums.length) {
8080
int temp = nums[i];

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