-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path47.permutations-ii.java
64 lines (61 loc) · 1.35 KB
/
47.permutations-ii.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode id=47 lang=java
*
* [47] Permutations II
*
* https://leetcode.com/problems/permutations-ii/description/
*
* algorithms
* Medium (39.19%)
* Likes: 1101
* Dislikes: 46
* Total Accepted: 257.8K
* Total Submissions: 623.2K
* Testcase Example: '[1,1,2]'
*
* Given a collection of numbers that might contain duplicates, return all
* possible unique permutations.
*
* Example:
*
*
* Input: [1,1,2]
* Output:
* [
* [1,1,2],
* [1,2,1],
* [2,1,1]
* ]
*
*
*/
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> answer = new ArrayList<>();
permute(nums, 0, answer);
return answer;
}
public void permute(int[] nums, int begin, List<List<Integer>> answer) {
if (begin == nums.length) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
answer.add(list);
} else {
HashSet<Integer> set = new HashSet<>();
for (int i = begin; i < nums.length; i++) {
if (set.contains(nums[i])) continue;
set.add(nums[i]);
swap(nums, i, begin);
permute(nums, begin + 1, answer);
swap(nums, i, begin);
}
}
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}