[leetcode] 47. 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],
[2,1,1]
] 和一般的Permutation不一样的是,这种permutation需要排序,使相同的元素能够相邻,选取下一个元素的时候,要查看这个元素的前一个元素是否和它相同,如果相同而且没有使用过,就不用选取这个元素,因为如果选取了这个元素,所得的结果被包含于选取了前一个相同元素的结果中。
代码如下:
public class Solution {
int length;
public List<List<Integer>> permuteUnique(int[] nums) {
length = nums.length;
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (length == 0) {
return result;
}
Arrays.sort(nums);
boolean[] flags = new boolean[length];
Arrays.fill(flags, false);
List<Integer> candidate = new ArrayList<Integer>();
helper(nums, length, flags, result, candidate);
return result;
}
private void helper(int[] nums, int n, boolean[] flags, List<List<Integer>> result, List<Integer> candidate) {
if (n == 0) {
result.add(new ArrayList<Integer>(candidate));
}
int ll = candidate.size();
for (int i = 0; i < length; i++) {
if (!flags[i]) {
//if the number appears more than once and the same number before it has not been used
if (i != 0 && nums[i] == nums[i - 1] && !flags[i - 1]) {
continue;
}
flags[i] = true;
candidate.add(nums[i]);
helper(nums, n - 1, flags, result, candidate);
candidate.remove(ll);
flags[i] = false;
}
}
}
}
[leetcode] 47. Permutations II的更多相关文章
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 47 Permutations II(全排列)
题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Alpha阶段测试报告
测试说明 APP中前后端交互的接口主要有两种,一种是游戏开始前获取信息的HTTP请求接口,这种接口可以看成是静态的,比较简单:另外一种就是游戏过程中进行实时通信的Websocket请求接口,因为这是在 ...
- Alpha阶段第六次Scrum Meeting
情况简述 Alpha阶段第六次Scrum Meeting 敏捷开发起始时间 2016/10/27 00:00 敏捷开发终止时间 2016/10/28 00:00 会议基本内容摘要 提出了目前阶段遇到的 ...
- C# winform开发:Graphics、pictureBox同时画多个矩形
C#的System.Drawing 命名空间提供了对 GDI+ 基本图形功能的访问 重点在于获取Graphics对象,例如: Graphics g = panel1.CreateGraphics 事实 ...
- thinkphp3.2与phpexcel解析
1.impot导入 第一种方式: import("Org.Util.PHPExcel.TextT"); $tt = new \TextT(); //创建PHPExcel对象,注意, ...
- Scene
Unity 中场景切换 http://www.cnphp6.com/archives/62868 场景管理插件Scene Manager http://blog.csdn.net/onerain88/ ...
- 关于linux asp.net MVC网站中 httpHandlers配置无效的处理方法
近期有Jexus用户反映,在Linux ASP.NET MVC网站的Web.config中添加 httpHandlers 配置用于处理自定义类型,但是在运行中并没有产生预期的效果,服务器返回了404( ...
- [NHibernate]事务
目录 写在前面 文档与系列文章 事务 增删改查 总结 写在前面 上篇文章介绍了nhibernate的增删改查方法及增加修改操作,这篇文章将介绍nhibernate的事务操作. SQL Server中的 ...
- IntersectionObserver API
温馨提示:本文目前仅适用于在 Chrome 51 及以上中浏览. 2016.11.1 追加,Firefox 52 也已经实现. 2016.11.29 追加,Firefox 的人担心目前规范不够稳定,未 ...
- NoClassDefFoundError vs ClassNotFoundException
我们先来认识一下Error 和Exception, 两个都是Throwable类的直接子类. Javadoc 很好的说明了Error类: An Error is a subclass of Thro ...
- HTML5+CSS3 - 代码简写篇
有话先说:我是一只菜鸟,还是一只刚步入前端这个领域的小菜年,在不断的进阶,理解最深刻的还是代码,既爱又恨却不知如何去感悟. background属性简写: background-position属性组 ...