【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 ...
随机推荐
- 飞天KEY
RoyCShell.exe -PE -if:"G:\EncryptTool\Finder.exe" -of:"G:\EncryptTool\Finder_enc.exe& ...
- Nuget server on IIS6 returns 404
Nuget server on IIS6 returns 404 when downloading package after upgrade 2011年9月2日 8:03:30 (GMT Dayli ...
- SDUT2013级測试赛_D
题目描写叙述 给出一棵含有n个点的树.每一个点权值为wi.求从根节点到叶子结点权值和最大的那条路经的权值和是多少. 输入 n(1<= n && n <= 10000). 接 ...
- Ajax技术——与服务器通信
1. 发送请求 Ajax可以通过XMLHttpRequest对象实现采用异步方式在后台发送请求.通常情况下,Ajax发送请求有两种,一种是发送GET请求,另一种是发送POST请求.但是无论发送哪种请求 ...
- django from组件 实现增加 删除 编辑(推荐用法)
实现效果: 代码示例: 代码: models.py from django.db import models class UserInfo(models.Model): username = mode ...
- 如何在Windows Server 2012 R2上安装SharePoint 2013
笔者原以为是个挺容易个事儿, 毕竟是微软自家的产品安装在自家的操作系统上, 没想到还是让我费了半天劲. 写在这里吧, 方便其他的朋友. 具体步骤 ======================= ...
- jsp+servlet+jdbc实现对数据库的增删改查
一.JSP和Servlet的简单介绍: 1.Servlet和JSP简介: Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP,Servlet是在服务器端执行的Java程序,一个 ...
- Hibernate之一对一关联映射
Hibernate中一对一关联映射共分为两种,一种是一对一主键关联映射,另一种是一对一唯一外键关联映射.下面简单介绍一下这两种关联映射. 一对一主键关联映射 一对一主键关联映射的两个实体有相同的ID. ...
- Logon database Tiger引起DataGuard不同步问题
当DataGuard升级CPU后.发现主备库不能同步了,日志报错,所以在主库的运行的升级的后脚本"@catbundle.sql cpu apply"不能同步到备库 现象: brok ...
- centos下docker网络桥接
停止服务 停止docker0网卡 Ip link set dev docker0 down 删除docker0 Brctl delbr docker0 进入到网卡的配置文件创建桥接网络br0 Brct ...