【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 ...
随机推荐
- linux automake 交叉编译
. ├── aclocal.m4 ├── autoscan.log ├── config.log ├── config.status ├── configure ├── configure.in ├─ ...
- 领扣-121/122/123/188 最佳买卖时机 Best Time to Buy and Sell MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Android4.2.2 Gallery2源码分析(2)——发现Gallery.java
上文中,main.xml是我直接提出来的,并没有说明是怎么找到它的,现在说明发现它的理由: 一般我们分析界面布局会用到hierarchyviewer这个工具,从工具中,我们对应到视图,最主要的视图id ...
- 用sc命令查询系统状态
用sc命令查询系统状态 sc query 语法 sc query - Enumerates status for active services & driver ...
- (linux shell)第二章--命令之乐(一)
文章来自于我的个人博客:(linux shell)第二章--命令之乐(一) 上一章我们描写叙述了一些linux shell中须要注意的一些语法.接下来我们開始了解linux shell的经常使用 ...
- HDU 1251 统计难题 (Trie)
pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/ ...
- ArcGIS Add-In调试无法重新生成
在调试ArcGIS Add-In时,出现错误:无法注册程序集"……\Projects\ArcGISAddIn\ArcGISAddIn\bin\Debug\ArcGISAddIn.dll&qu ...
- 利用反射实现JavaBean的自动赋值
最近开发公司的一个项目,因项目不是很大的项目,所以仅仅采用了spring MVC框架,但是数据库很多表中的字段至少15个,这样当每次将数据库表中的数据取出来放入javabean中时,写setXXX方法 ...
- RMSE均方根误差学习笔记
1.均方根误差,它是观测值与真值偏差的平方和观测次数n比值的平方根,在实际测量中,观测次数n总是有限的,真值只能用最可信赖(最佳)值来代替.方根误差对一组测量中的特大或特小误差反映非常敏感,所以,均方 ...
- MySQL 中now()时间戳用法
MySQL 中now()时间戳用法 UPDATE news set addtime = unix_timestamp(now()); #结果:1452001082