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. ...
随机推荐
- Linux下查看磁盘挂载的三种方法
Linux下查看磁盘挂载的三种方法 2009-06-05 23:17 好久没有更新日志了,呵呵.不是没有要写的东东.实在抽不出时间来写,要准备公司的考试呢,C++考试.已经有七个月没有写C++代码了, ...
- CSS:给 input 中 type="text" 设置CSS样式
input[type="text"], input[type="password"] { border: 1px solid #ccc; paddi ...
- atitit.资源释放机制--attilax总结
atitit.资源释放机制--attilax总结 1. .全手工, 1 2. 引用计数, 1 2.1. 成本也显而易见. 1 2.2. 循环引用的问题, 2 2.3. 引用计数方式事实上也有经典的卡顿 ...
- 使用sqlplus连接提示:ORA-28002: the password will expire within 7 days
今天在使用sqlplus时出现,或使用数据库的时候出现类似问题 =============================================== ERROR:ORA-28002: the ...
- 以源码编译的方式安装PHP与php-fpm
首先是最基本的下载,解压,编译安装(以PHP 5.3.6 为例): wget http://www.php.net/get/php-5.3.6.tar.gz/from/this/mirrortar x ...
- php比较函数,判断安全函数
一.字符串比较函数: int strcasecmp ( string $str1 , string $str2 ) int strcmp ( string $str1 , string $str2 ) ...
- QSettings 使用实例 当需要在程序关闭时保存”状态“信息
用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小,位置,一些别的设置,还有一个经常用的,就是recent files,等等这些都可以通过Qsettings来实现. 我们知 ...
- mysql操作及自动化运维
备份恢复工具:percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm mysql主从配置命令: 主: 1.编辑主MYSQL 服务器的MySQL配置文件my.cnf, ...
- Python黑魔法,一行实现并行化
Python 在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和 GIL,我觉得错误的教学指导才是主要问题.常见的经典 Python 多线程.多进程教程多显得偏“重”.而且往往隔靴搔 ...
- 用C语言(apue)实现 把时间戳转换为国标格式的字符串(2017-07-17 22:36:12)的函数
/*******************************************************************************/ /** *** 函 数 名: cha ...