【题目】

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 解题报告的更多相关文章

  1. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  2. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  3. LeetCode: N-Queens II 解题报告

    N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem.

  4. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  5. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  6. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  7. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  9. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. [Android Pro] android root权限破解分析

    许 多机友新购来的Android机器没有破解过Root权限,无法使用一些需要高权限的软件,以及进行一些高权限的操作,其实破解手机Root权限是比较简 单及安全的,破解Root权限的原理就是在手机的/s ...

  2. 如何查看ESXi的网卡的MAC地址?

    直接上图 图一, 物理网卡   图二, vmkernel虚拟网卡   参考资料 ============================ How To Determine Vmkernel Inter ...

  3. java后台与jsp前台特殊字符处理(字符串编码与解码)

    在后台与前台数据交互时如果有特殊字符就很容易出现问题,所以就需要对字符串进行编码传输,在获取后再进行解码: 1.Java后台进行编码与解码 URLEncoder.encode(str,"ut ...

  4. (队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)

    /* * ZOJ_3210.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  5. 微信-.NET调用JS-SDK

    注意:1. 所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”.2. 如果发现在 Android 不能分享自定义内 ...

  6. 再谈Cognos利用FM模型来做同比环比

    很早之前已经讲过 <Cognos利用DMR模型开发同比环比>这篇文章里说的是不利用过滤器,而是采用 except (lastPeriods (-9000,[订单数据分析].[日期维度].[ ...

  7. Visual Studio 2015年预览设置: 辅助安装程序说明

    本文介绍了第三方应用程序安装辅助安装的 Visual Studio 2015年预览时安装的说明.如果您安装了多设备开发功能,您需要使用其他第三方软件来处理这些项目.辅助安装程序允许您将部署到您的计算机 ...

  8. 安装Was liberty之步骤

    安装文件下载:http://pan.baidu.com/s/1dDl8PuL 安装三大步骤:拷贝文件,安装VNC和安装WasLiberty 拷贝文件是将需要的文件InstalMgr1.6.2_LNX_ ...

  9. Makefile 编译静态库

    CC = gcc AR = ar FLAGS = -Wall -lpthread CLOUD = cloud_server OBJ += cloud_server.o LIB = libccloud. ...

  10. Discuz常见小问题-如何禁止用户发言,快速删除某个用户的所有帖子

    用户-用户组,勾选批量编辑,然后点击批量编辑的链接   点击论坛相关-帖子相关,然后把指定用户组的允许发新话题设置为否,拉到底部,点击提交   以一个普通用户重新登录,尝试发帖报错,说明已经设置成功 ...