forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermutationsII.java
40 lines (35 loc) · 1.06 KB
/
PermutationsII.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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
* 需要研究
*/
public class PermutationsII {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
permute(nums, result, 0);
return result;
}
public void permute(int[] nums, List<List<Integer>> result, int start) {
if (start >= nums.length) {
List<Integer> list = new ArrayList<Integer>();
for (Integer n : nums) {
list.add(n);
}
result.add(list);
}
HashSet<Integer> set = new HashSet<Integer>();
for (int i = start; i < nums.length; i++) {
if (set.add(nums[i])) {
swap(nums, start, i);
permute(nums, result, start + 1);
swap(nums, start, i);
}
}
}
public static void swap(int[] nums, int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}