【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 ...
随机推荐
- UIButton 设置圆角 边框颜色 点击回调方法
UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom]; signBtn.frame = CGRectMake(, , , ) ...
- 升级struts到2.5.2遇到的问题及解决方案
原来的版本是2.3.x,由于安全原因需要升级到2.5.2.1,2.5.2版本不再提供xwork.jar ,整合到了 struts-core包中. 2,方法不能访问的问题,需要在每个action配置文件 ...
- C语言指针加1问题以及字节对齐问题
今天早上自己写了一段代码,然后测试的时候发现结果总是和预期的不一样,而且偏差的有点离谱,冥思苦想了将近五个小时,最后在我要开始怀疑人生的时候,发现原来是自己犯了一个极其低级但又容易被忽略的问题.好吧, ...
- 如何更换Office 2013的product key?
第一步 第二步 第三步 ... ... ... ... ^_^ 参考资料 ======================== Change Product Key Office 2013 Home ...
- [NPM] Avoid Duplicate Commands by Calling one NPM Script from Another
We can get a lot of utility through CLI tools invoked via npm scripts. Many of these tools have APIs ...
- CSS中:before和:after选择器的用法
在线演示这次给大家带来的是对话气泡效果,主要是演示了 :before / :after 和 border 的用法,赶快来围观吧. 阅读原文:CSS中:before和:after选择器的用法
- nginx的 CPU參数worker_processes和worker_cpu_affinity使用说明
Nginx默认没有开启利用多核CPU,我们能够通过添加worker_cpu_affinity配置參数来充分利用多核CPU.CPU是任务处理,计算最关键的资源,CPU核越多.性能就越好. worker_ ...
- angular中$location读取url信息
读取url信息: <html ng-app="myApp"> <head> <title>angularjs-demo</title> ...
- HDU1004——Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- Myeclipse2013下载,安装,破解,介绍(CSDN首发)
MyEclipse 2013新特性 根据官方最新消息,MyEclipse 2013已经正式发布!MyEclipse 2013支持HTML5.JQuery和主流的Javascript 库. 随着MyEc ...