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. ...
随机推荐
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- php回调函数call_user_func和call_user_func_array详解
call_user_func($fun); call_user_func 函数类似于一种特别的调用函数的方法,使用方法如下: 1.调用 普通函数: <?php function a($b, $c ...
- svn move (mv,rename,ren)
svn 重命名文件: [root@NGINX-APACHE-SVN pro]# svn move 20160624新建数据库表.txt 201.txt A 201.txt D 20160624新建数据 ...
- ELK-“线上标准文档”——测试
Elasticstack官网:https://www.elastic.co 本文档仅限搭建过程参考,使用相关的文档,不在本文档讨论范围之内. 一切依据的核心即是Elasticstack官网. 查看支持 ...
- 【转】oozie安装和自带示例的使用
oozie安装 [转]http://www.tuicool.com/articles/qUVNJn oozie自带示例的使用 [转]http://blog.csdn.net/zhu_xun/artic ...
- js 静态方法 静态变量 实例方法 实例变量
1.静态方法的定义 Js代码 var BaseClass = function() {}; // var BaseClass=new Function(); BaseClass.f1 = func ...
- nginx日志自动切割
1.日志配置 Nginx 日志分 access.log 记录哪些用户,哪些页面以及用户浏览器,IP等访问信息: error.log 记录服务器错误的日志 配置日志存储路径 location / { a ...
- Java之旅hibernate(8)——基本关系映射
何为关系,何为映射,关系这个词想必大家都不陌生.比方你和老师之间是师生关系,你和父母之间是父子或者父女(母子或者母女关系). 关系是存在某种联系物体之间产生的.什么都是可能的.比方你和工具,你仅仅能使 ...
- Linux开机启动文件rc.local无法执行怎么办?
rc.local是Linux系统中的一个重要的开机启动文件,每次开机都要执行这个文件.但是有一些用户的Linux系统无法执行这个文件,并导致了一系列的问题.遇到这个问题我们应该怎么办呢? 在Linux ...
- hdu6121 Build a tree 模拟
/** 题目:hdu6121 Build a tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:n个点标号为0~n-1:节点i的父节点 ...