PermutationsII

全排列 II

题目介绍

全排列 II

给定一个可包含重复数字的序列 nums按任意顺序 返回所有不重复的全排列。

示例1:

1
2
3
4
5
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]

示例2:

1
2
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

题目解法

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 algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PermutationsII {
static boolean[] vis;

public static List<List<Integer>> permuteUnique(int[] nums) {

List<List<Integer>> ans = new ArrayList<>();
List<Integer> perm = new ArrayList<>();
vis = new boolean[nums.length];
Arrays.sort(nums);
backtrack(nums, ans, 0, perm);
return ans;

}

public static void backtrack(int[] nums, List<List<Integer>> ans, int idx, List<Integer> perm) {
if (idx == nums.length) {
ans.add(new ArrayList<>(perm));
return;
}
for (int i = 0; i < nums.length; ++i) {
// 从左往右第一个未被填过的数字
if (vis[i] || (i > 0 && nums[i] == nums[i - 1] && !vis[i - 1])) {
continue;
}
perm.add(nums[i]);
vis[i] = true;

backtrack(nums, ans, idx + 1, perm);

vis[i] = false;
perm.remove(idx);
}
}

public static void main(String[] args) {
int[] nums = {1,1,2};
System.out.println(permuteUnique(nums));

nums = new int[]{1,2,3};
System.out.println(permuteUnique(nums));
}
}

打印:

1
2
[[1, 1, 2], [1, 2, 1], [2, 1, 1]]
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

思路:

思路上,使用回溯,排序然后排重。


PermutationsII
https://yangtzeshore.github.io/2021/03/12/PermutationsII/
作者
Chen Peng
发布于
2021年3月12日
许可协议