全排列 II

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

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutations-ii/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:穷举法
  • 首先,构造一棵多叉树MultiTree,该多叉树有以下几个属性,used表示当前路径已经走过的数组的位置,paths表示当前路径中的数字。
  • 然后声明一个队列queue,队列的元素就是MultiTree,首先将nums中不同的数字出初始化成路径的第一个数字,然后加入到队列中(需要同时初始化used和paths)。
  • 然后遍历队列queue,按照类似的方式将数组nums中没用到的数字加入到当前路径中(需要判断重复数字)。
  • 直到队列中每一条路径的长度都和nums的长度一样,即已将所有的数字加入到路径中。
  • 最后,返回队列中的所有的路径paths。

说明:其实本来想构造一棵多叉树,所有叶子节点到根节点的路径即为所有的路径排列,后来没用到,所以没有构造树的父子关系 。

  1. import java.util.*;
  2. public class LeetCode_047 {
  3. /**
  4. * 构造一棵多叉树
  5. */
  6. static class MultiTree {
  7. // 当前的值
  8. public Integer val;
  9. public MultiTree parent;
  10. // 当前路径已经走过的数组的位置
  11. public List<Integer> used;
  12. // 当前路径中的数字
  13. public List<Integer> paths;
  14. public MultiTree(Integer val) {
  15. this.val = val;
  16. used = new ArrayList<>();
  17. paths = new ArrayList<>();
  18. }
  19. }
  20. public static List<List<Integer>> permuteUnique(int[] nums) {
  21. Queue<MultiTree> queue = new LinkedList<>();
  22. Arrays.sort(nums);
  23. int curNum = nums[0];
  24. // 第一条路径
  25. MultiTree first = new MultiTree(nums[0]);
  26. first.paths.add(nums[0]);
  27. first.used.add(0);
  28. queue.add(first);
  29. // 其他路径
  30. for (int i = 1; i < nums.length; i++) {
  31. if (nums[i] != curNum) {
  32. MultiTree next = new MultiTree(nums[i]);
  33. next.paths.add(nums[i]);
  34. next.used.add(i);
  35. queue.add(next);
  36. curNum = nums[i];
  37. }
  38. }
  39. int length = 1;
  40. while (length < nums.length) {
  41. int count = queue.size();
  42. while (count > 0) {
  43. MultiTree curNode = queue.poll();
  44. int firstNum = -1, firstNumIndex = -1;
  45. // 找到第一个已有路径没经过的数
  46. for (int i = 0; i < nums.length; i++) {
  47. if (!curNode.used.contains(i)) {
  48. firstNum = nums[i];
  49. firstNumIndex = i;
  50. MultiTree firstTree = new MultiTree(nums[i]);
  51. firstTree.paths.addAll(curNode.paths);
  52. firstTree.paths.add(firstNum);
  53. firstTree.used.addAll(curNode.used);
  54. firstTree.used.add(firstNumIndex);
  55. queue.add(firstTree);
  56. break;
  57. }
  58. }
  59. // 将其他不同的数也添加到新的路径
  60. for (int i = firstNumIndex + 1; i < nums.length; i++) {
  61. if (!curNode.used.contains(i) && nums[i] != firstNum) {
  62. MultiTree otherTree = new MultiTree(nums[i]);
  63. otherTree.paths.addAll(curNode.paths);
  64. otherTree.paths.add(nums[i]);
  65. otherTree.used.addAll(curNode.used);
  66. otherTree.used.add(i);
  67. queue.add(otherTree);
  68. firstNum = nums[i];
  69. }
  70. }
  71. count--;
  72. }
  73. length++;
  74. }
  75. List<List<Integer>> result = new ArrayList<>();
  76. while (!queue.isEmpty()) {
  77. result.add(queue.poll().paths);
  78. }
  79. return result;
  80. }
  81. public static void main(String[] args) {
  82. int[] nums = new int[]{1, 1, 2};
  83. for (List<Integer> integers : permuteUnique(nums)) {
  84. for (Integer integer : integers) {
  85. System.out.print(integer + " ");
  86. }
  87. System.out.println();
  88. }
  89. }
  90. }

【每日寄语】 愿太阳的光辉始终洒在你心上。愿所有的不愉快,苦尽甘来。愿每个脆弱的人都能得到善待。愿现实有光,世界有暖。