leetcode -- Permutations II TODO
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].
解题思路:
Permutations 那题输入是不包含重复元素的,故生成的排列都是不同的,II中输入元素可能相同
因而可能会产生相同的排列,这里需要去重,没有找到很好的剪枝条件,这里使用HashSet来去重,
当发现已经添加过该组合则不会再添加
public boolean add(E e)
如果此 set 中尚未包含指定元素,则添加指定元素。
更确切地讲,如果此 set 没有包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则向此 set 添加指定的元素 e。
如果此 set 已包含该元素,则该调用不更改 set 并返回 false。
public class Solution {
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
int len = num.length;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
HashSet<ArrayList<Integer>> tmp = new HashSet<ArrayList<Integer>>();
//Arrays.sort(num);
permutation(num, 0, len, result, tmp);
return result;
}
public void permutation(int[] num, int depth, int len,
ArrayList<ArrayList<Integer>> result, HashSet<ArrayList<Integer>> tmpResult){
if(depth == len){
ArrayList<Integer> per = new ArrayList<Integer>();
for(int i =0 ; i < len; i++)
per.add(num[i]);
if(tmpResult.add(per))
result.add(per);
}
for(int i = depth; i < len; i++) {
//if(i != depth && num[i] == num[depth])
// continue;
int tmp = num[i];
num[i] = num[depth];
num[depth] = tmp;
permutation(num, depth + 1, len, result, tmpResult);
tmp = num[i];
num[i] = num[depth];
num[depth] = tmp;
}
}
}
leetcode -- Permutations II TODO的更多相关文章
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- [Leetcode] permutations ii 全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode Permutations II (全排列)
题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
随机推荐
- Cordova 快速入门记录
本篇文章由:http://xinpure.com/cordova-quick-start-recording/ 记一笔 Cordova 官网入门文档 Get Started Fast,言简意该.通俗易 ...
- Android SDK 快速安装方法
我们都知道使用Android sdk manager下载安装sdk速度非常慢,一般在10k/s以内,本文章推荐一种能够借助迅雷等下载工具下载sdk的zip包从而快速安装sdk的方法. 1.下载3个xm ...
- 【转载】Redis在windows下安装过程
一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...
- Linux-软件包管理-rpm命令管理-查询
rpm -q httpd 查看apache包是否已经安装 rpm -qa 查看所有已经安装的包rpm -qa | grep httpd 查询包含和apache关键字相关联的所有包信息 rpm -qi ...
- Redis全方位讲解--哨兵模式(Sentinel模式)(转载)
前言 当按照上一篇<redis主从复制>部署好之后,我们会想,一旦redis的master出现了宕机,并且我们并没有及时发现,这时候就可能会出现数据丢失或程序无法运行.此时,redis的哨 ...
- Android 中 字符串比较
EditText中getText().toString() 得到的字符串 写法如下if(m_txtAddress.getText().toString()=="") 这样写 是不会 ...
- MVC与MVP(转)
MVC模式已经出现了几十年了,在GUI领域已经得到了广泛的应用,由于微软ASP.NET MVC Framework的出现,致使MVC一度成为.NET社区的热名话题.作为MVC的变种MVP模式,也已经出 ...
- unity, Rigidbody.constraints
一,同时施加多个限制: 用按位或(bitwise OR)实现,例如: GetComponent<Rigidbody>().constraints=RigidbodyConstraints. ...
- eclipse下修改默认编码
window-preferences-右侧选项卡-General-Workspace-Text file encoding
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...