-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationsII.java
More file actions
48 lines (40 loc) · 1.34 KB
/
PermutationsII.java
File metadata and controls
48 lines (40 loc) · 1.34 KB
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
package medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* ClassName: PermutationsII
* @author chenyiAlone
* Create Time: 2018/12/14 10:35:40
* Description: No.47 无重复的全排列,处理重复的时候的时间复杂度太差,基本可以废弃
*/
public class PermutationsII {
public List<List<Integer>> permuteUnique(int[] nums) {
Integer[] ansInt = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
ansInt[i] = Integer.valueOf(nums[i]);
}
List<List<Integer>> ans = new ArrayList<List<Integer>>();
permute(ansInt, 0, ans);
return ans;
}
public static void permute(Integer[] nums, int p, List<List<Integer>> list) {
if (p == nums.length - 1) {
List<Integer> ans = new ArrayList(Arrays.asList(nums));
if (list.contains(ans)) {
return;
}
list.add(ans);
}
for (int i = p; i < nums.length; i++) {
Integer temp = nums[p];
nums[p] = nums[i];
nums[i] = temp;
permute(nums, p + 1, list);
temp = nums[p];
nums[p] = nums[i];
nums[i] = temp;
}
}
}