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的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. LeetCode: Permutations II 解题报告

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

  3. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. [Leetcode] Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. [leetcode]Permutations II @ Python

    原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...

  6. [Leetcode] permutations ii 全排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. [LeetCode] Permutations II 排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. LeetCode Permutations II (全排列)

    题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...

  9. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

随机推荐

  1. Ubuntu系统使用命令禁用触摸板等输入设备

    [日期:2012-02-22]   本人用的Ubuntu 10.04系统,笔记本上有鼠标和触摸板.默认下,触摸板是开启的, 有时候打字的时候会不时碰到触摸板,添了不少麻烦,所以得禁用触摸板,限于目前所 ...

  2. weblogic设置jvm参数

    http://www.quiee.com.cn/archives/592/ weblogic a) 编辑Weblogic Server启动脚本文件:BEA_HOME\user_projects\dom ...

  3. GitHub上最火的Android开源项目(完结篇)

    摘要:截至目前,在GitHub“最受欢迎的开源项目”系列文章中我们已介绍了40个Android开源项目,对于如此众多的项目,你是Mark.和码友分享经验还是慨叹“活到老要学到老”?今天我们将继续介绍另 ...

  4. Linux命令-目录处理命令:rm

    rm grub.conf 删除当前目录中grub.conf文件 (最好是先复制一个文件,然后再删除,这样保险) rm -f grub.conf 强制删除当前目录中的grub.conf文件 (不需要确认 ...

  5. C++:CursorType光标类型 和 LockType锁定类型

    简要: CursorType光标类型: 1.   AdOpenForwardOnly   (默认值)一次只能向前移动一行. 2.   AdOpenKeyset   打开键集类型游标. 3.   AdO ...

  6. hook NtReadVirtualMemory干扰杀软扫描

      信息来源:邪恶八进制信息安全团队(www.eviloctal.com)文章作者:asm(http://www.sbasm.cn) 写了个对抗扫描的东西,跟大家分享!技术含量不高,大牛飘过.一直以来 ...

  7. 黑马程序猿——25,打印流,合并流,对象序列化,管道流,RandomAccessFile

    ------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...

  8. Atitit. Dwr 抛出异常error解决方案

    Atitit. Dwr 抛出异常error解决方案 1. Dwr3的处理机制..setErrorHandler 1 2. remote Mteh  try catch 1 3. 林吧,子好java 处 ...

  9. [svc]expect的爱恨情仇

    背景 openvpn生成证书想把它做成一键化,这样添加新用户时候就方便 遇到的问题 我的代码 gg_vpn_keys.exp #!/usr/bin/expect set user [lindex $a ...

  10. Linux服务器丢包故障的解决思路及引申的TCP/IP协议栈理论

    我们使用Linux作为服务器操作系统时,为了达到高并发处理能力,充分利用机器性能,经常会进行一些内核参数的调整优化,但不合理的调整常常也会引起意想不到的其他问题,本文就一次Linux服务器丢包故障的处 ...