全排列 II

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

示例说明请见LeetCode官网。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/permutations-ii/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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

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

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();
}
}
}

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

LeetCode-047-全排列 II的更多相关文章

  1. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  2. [LeetCode] 47. 全排列 II

    题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...

  3. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  4. Java实现 LeetCode 47 全排列 II(二)

    47. 全排列 II 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] class Solut ...

  5. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. LeetCode 47 全排列II

    题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...

  7. LeetCode 47. 全排列 II(Permutations II)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...

  8. LeetCode 047 Permutations II

    题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...

  9. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  10. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

随机推荐

  1. django之百度Ueditor富文本编辑器后台集成

    Python3 + Django2.0 百度Ueditor 富文本编辑器的集成 百度富文本编辑器官网地址:http://fex.baidu.com/ueditor/ 疑问:为什么要二次集成? 答案:因 ...

  2. Block内存管理

    block是不是一个对象?是一个对象 如何判断当前文件是MRC,还是ARC 1.dealloc 能否调用super,只有MRC才能调用super 2.能否使用retain,release.如果能用就是 ...

  3. idea的jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attribute

    感谢大佬:https://blog.csdn.net/mingyuli/article/details/84674483 命令行运行jar出现问题: 1.找不到主类.打开jar文件包,在MANIFES ...

  4. PHP版的猴子选大王算法

    猴子选大王 这个算法可能是目前我看到的最简洁都算法吧,而且很好理解.它不同于其他算法,其他算法都是判断这个猴子能不能被选中,而他只是找出不能被选中的猴子,然后将其塞到数组模拟的环状队列中,参与下次选. ...

  5. CentOS 6.4x64安装部署zabbix-2.4.5

    以下内容来自于http://www.iyunv.com/thread-62087-1-1.html 补充一点,按照原文安装万之后zabbix页面会提示 zabbix server is not run ...

  6. 使用MediaPlayer框架实现简单音乐播放器-陈棚

    该项目需要使用MediaPlayer框架,因此程序需要先为该项目添加MediaPalyer框架,并在上面控制器类的实现部分使用#import<MediaPlayer/MediaPlayer.h& ...

  7. java程序出现oom如何解决?什么场景下会出现oom?

    1.什么是oom? OOM,全称"Out Of Memory",翻译成中文就是"内存用完了",来源于java.lang.OutOfMemoryError. 当J ...

  8. linux_3

    1.统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来 [root@lhq ~]#echo "total:`cat /etc/pa ...

  9. Innodb之索引与算法

    目录 一.概述 二.数据结构与算法 1.二分查找 2.二叉查找树和平衡二叉树 1)二叉查找树 2)平衡二叉树 三.B+树 1.B+树完整定义 2.关于 M 和 L的选定案例 四.B+树索引 1.聚集索 ...

  10. P3400【仓鼠窝 】

    思路清奇,代码简洁的好题 问题大体分两部: 记录子矩阵个数,统计每一个点作为右下角时可以得到多少矩形,加起来就是答案 剪掉墙挡住的地方 考虑从右下角开始,如果有0已经挡住了矩阵,那么更靠左.上的0都卵 ...