Full arrangement II
Title description: Given a sequence nums that can contain repeated numbers, return all non-repeated full permutations in any order.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/permutations-ii/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Exhaustive method
- First, construct a multi-tree MultiTree. The multi-tree has the following properties. Used represents the position of the array that the current path has traversed, and paths represents the number in the current path.
- Then declare a queue, the element of the queue is MultiTree, first initialize the different numbers in nums to the first number of the path, and then add it to the queue (need to initialize used and paths at the same time).
- Then traverse the queue and add unused numbers in the array nums to the current path in a similar manner (repeated numbers need to be judged).
- Until the length of each path in the queue is the same as the length of nums, that is, all numbers have been added to the path.
- Finally, all paths in the queue are returned.
Description of actually wanted to construct a multi-branch tree. The path from all leaf nodes to the root node is the arrangement of all paths, which is not used later, so there is no parent-child relationship for constructing the tree.
import java.util.*;
public class LeetCode_047 {
/**
* 构造一棵多叉树
*/
static class MultiTree {
// 当前的值
public Integer val;
public MultiTree parent;
// 当前路径已经走过的数组的位置
public List<Integer> used;
// 当前路径中的数字
public List<Integer> paths;
public MultiTree(Integer val) {
this.val = val;
used = new ArrayList<>();
paths = new ArrayList<>();
}
}
public static List<List<Integer>> permuteUnique(int[] nums) {
Queue<MultiTree> queue = new LinkedList<>();
Arrays.sort(nums);
int curNum = nums[0];
// 第一条路径
MultiTree first = new MultiTree(nums[0]);
first.paths.add(nums[0]);
first.used.add(0);
queue.add(first);
// 其他路径
for (int i = 1; i < nums.length; i++) {
if (nums[i] != curNum) {
MultiTree next = new MultiTree(nums[i]);
next.paths.add(nums[i]);
next.used.add(i);
queue.add(next);
curNum = nums[i];
}
}
int length = 1;
while (length < nums.length) {
int count = queue.size();
while (count > 0) {
MultiTree curNode = queue.poll();
int firstNum = -1, firstNumIndex = -1;
// 找到第一个已有路径没经过的数
for (int i = 0; i < nums.length; i++) {
if (!curNode.used.contains(i)) {
firstNum = nums[i];
firstNumIndex = i;
MultiTree firstTree = new MultiTree(nums[i]);
firstTree.paths.addAll(curNode.paths);
firstTree.paths.add(firstNum);
firstTree.used.addAll(curNode.used);
firstTree.used.add(firstNumIndex);
queue.add(firstTree);
break;
}
}
// 将其他不同的数也添加到新的路径
for (int i = firstNumIndex + 1; i < nums.length; i++) {
if (!curNode.used.contains(i) && nums[i] != firstNum) {
MultiTree otherTree = new MultiTree(nums[i]);
otherTree.paths.addAll(curNode.paths);
otherTree.paths.add(nums[i]);
otherTree.used.addAll(curNode.used);
otherTree.used.add(i);
queue.add(otherTree);
firstNum = nums[i];
}
}
count--;
}
length++;
}
List<List<Integer>> result = new ArrayList<>();
while (!queue.isEmpty()) {
result.add(queue.poll().paths);
}
return result;
}
public static void main(String[] args) {
int[] nums = new int[]{1, 1, 2};
for (List<Integer> integers : permuteUnique(nums)) {
for (Integer integer : integers) {
System.out.print(integer + " ");
}
System.out.println();
}
}
}
【Daily Message】 May the brilliance of the sun always shine on your heart. May all the unpleasantness come to you. May every vulnerable person be treated kindly. May there be light in reality and warmth in the world.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。