【LeetCode】Permutations II 解题报告
【题目】
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1],
and [2,1,1].
【解析】
题意:求一个数组的全排列。与【LeetCode】Permutations 解题报告 不同的是,数组中的数有反复。
对于全排列,如今比較经常使用的算法就是依据 【LeetCode】Next Permutation 解题报告 从小到大逐个找出全部的排列。
算法:回溯、字典序法。
public class Solution {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> permuteUnique(int[] num) {
Arrays.sort(num);
//首先得把原始数组加入到结果集
List<Integer> list = new ArrayList<Integer>();
for (int x : num) {
list.add(x);
}
ans.add(list);
//逐个加入下一个解
for (int i = 1; i < factorial(num.length); i++) {
nextPermutation(num);
}
return ans;
}
public void nextPermutation(int[] num) {
//找到最后一个正序
int i = num.length - 1;
while (i > 0 && num[i] <= num[i - 1]) {
i--;
}
if (i <= 0) return;
//找到最后一个比num[i-1]大的数
int j = num.length - 1;
while (j >= i && num[j] <= num[i - 1]) {
j--;
}
//交换
int tmp = num[i - 1];
num[i - 1] = num[j];
num[j] = tmp;
//逆排i-1之后的数
int l = i, r = num.length - 1;
while (l < r) {
tmp = num[l];
num[l] = num[r];
num[r] = tmp;
l++;
r--;
}
//加入到结果集
List<Integer> list = new ArrayList<Integer>();
for (int x : num) {
list.add(x);
}
ans.add(list);
}
public int factorial(int n) {
return n == 0 ?
1 : n * factorial(n - 1);
}
}
相关题目:【LeetCode】Permutations 解题报告 和 【LeetCode】Next Permutation
解题报告
【LeetCode】Permutations II 解题报告的更多相关文章
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- LeetCode: N-Queens II 解题报告
N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem.
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- 《C++反汇编与逆向分析技术揭秘》之十——析构函数
局部对象 当对象所在作用域结束之后,销毁栈空间,此时析构函数被调用. 举例: 函数返回时自动调用析构函数: 堆对象 调用析构代理函数来处理析构函数: 为什么使用析构代理函数来调用析构函数?考虑到如果d ...
- Windows之权限讲解
windows中,权限指的是不同账户对文件,文件夹,注册表等的访问能力.在windows中,为不同的账户设置权限很重要,可以防止重要文件被其他人所修改,使系统崩溃. 1权限概念 我们可以在控制面板中设 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- CentOS7 常用命令集合
CentOS7 常用命令集合 文件与目录操作 touch test.txt: 创建一个文本文件 文本内容处理 查询操作 压缩.解压 yum安装器 网络相关 系统相关 XSheel 5相关操作 窗体快捷 ...
- 使用webService时,gsoap数据类型注意事项
今天在使用gsoap生成webservice客户端文件时,发现我的参数类型全被改了,比如string型变成了char*,原来有STL的地方也变没了,经过研究发现,原来和我生成的头文件时使用的参数有关, ...
- Tapable 0.2.8 入门
[原文:Tapable 0.2.8 入门] tapable是webpack的核心框架(4.0以上版本的API已经发生了变化),是一个基于事件流的框架,或者叫做发布订阅模式,或观察者模式,webpack ...
- 已知m和n是两个整数,并且m^2+mn+n^2能被9整除,试证m,n都能被3整除。
引证:m,n都是整数,m2=3n,求证m是3的倍数. 引证证明:(反证法)假设m并非3的倍数,那么m2则不含因数3,则m2≠3n,这与已知条件相反. 所以,当m2=3n时,m必是3的倍数. 有了引证, ...
- 深入一点 让细节帮你和Fragment更熟络
有一段时间没有写博客了.作为2017年的第一篇,初衷起始于前段时间一个接触安卓开发还不算太长时间的朋友聊到的一个问题: "假设,想要对一个Fragment每次在隐藏/显示之间做状态切换时进行 ...
- Javascript的解析器
Carakan C/C++ http://my.opera.com/core/blog/2009/02... SquirrelFish C++ http://trac.webkit.org/wiki/ ...
- windows下基于apache的SVN启动失败修改
我用的svn版本是:Setup-Subversion-1.8.1-1.msi, Apache版本是httpd-2.2.25-win32-x86-no_ssl.msi,安装完后把SVN bin文件夹下的 ...