/
_0047_PermutationsII.java
59 lines (51 loc) · 1.91 KB
/
_0047_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.diguage.algorithm.leetcode;
import java.util.*;
/**
* = 47. Permutations II
*
* https://leetcode.com/problems/permutations-ii/[Permutations II - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-27 20:29
*/
public class _0047_PermutationsII {
/**
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Permutations II.
* Memory Usage: 41.6 MB, less than 11.94% of Java online submissions for Permutations II.
*/
public List<List<Integer>> permuteUnique(int[] nums) {
if (Objects.isNull(nums) || nums.length == 0) {
return Collections.emptyList();
}
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
List<List<Integer>> result = new ArrayList<>();
backtrack(nums, 0, used, result, new ArrayDeque<>());
return result;
}
private void backtrack(int[] nums, int startIndex, boolean[] used,
List<List<Integer>> result, Deque<Integer> current) {
if (nums.length == startIndex) {
result.add(new ArrayList<>(current));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
// 修改 2:在 used[i - 1] 刚刚被撤销的时候剪枝,说明接下来会被选择,搜索一定会重复,故"剪枝"
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) {
continue;
}
used[i] = true;
current.addLast(nums[i]);
backtrack(nums, startIndex + 1, used, result, current);
current.removeLast();
used[i] = false;
}
}
}
public static void main(String[] args) {
_0047_PermutationsII solution = new _0047_PermutationsII();
int[] n1 = {1, 1, 2};
System.out.println(solution.permuteUnique(n1));
}
}