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].

Solution:

 public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
Arrays.sort(num);
List<List<Integer>> result=new ArrayList<List<Integer>>();
List<Integer> al=new ArrayList<Integer>();
boolean[] flag=new boolean[num.length];
dfs(num,result,al,0,flag);
return result;
} private void dfs(int[] num, List<List<Integer>> result, List<Integer> al, int level,boolean[] flag) {
// TODO Auto-generated method stub
if(level>num.length)
return;
if(level==num.length){
result.add(new ArrayList<Integer>(al));
return;
}
for(int i=0;i<num.length;++i){
if(!flag[i]){
flag[i]=true;
al.add(num[i]);
dfs(num, result, al, level+1, flag);
al.remove(al.size()-1);
flag[i]=false;
while((i+1<num.length)&&num[i]==num[i+1])
++i;
}
}
}
}

[Leetcode] Permutations II的更多相关文章

  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 @ Python

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

  5. leetcode -- Permutations II TODO

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

  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. 设计模式学习之观察者模式(Observer,行为型模式)(7)

    1.观察者模式又叫做发布-订阅模式. 2.观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 3 ...

  2. Delphi中线程类TThread实现多线程编程1---构造、析构……

    参考:http://www.cnblogs.com/rogee/archive/2010/09/20/1832053.html Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大 ...

  3. Python 写Windows Service服务程序

    1.需求 为什么要开发一个windows服务呢?之前做一个程序,必须要读取指定目录文件License, 因为其他程序也在读取这指定目录的License文件,且License不同时会修改License的 ...

  4. 使用JDBC的addBatch()方法提高效率

    在批量更新SQL操作的时候建议使用addBatch,这样效率是高些,数据量越大越能体现出来 Statement接口里有两个方法:void     addBatch(String sql)将给定的 SQ ...

  5. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏

    1.HTML部分:     <form id="form1" runat="server">     <script src=".. ...

  6. 信号量进程同步,王明学learn

    信号量进程同步 一组并发进程进行互相合作.互相等待,使得各进程按一定的顺序执行的过程称为进程间的同步. 信号量在进程同步时初始值为:0 信号量在进程互斥时初始值为:大于0的 本章节主要使用信号量,使的 ...

  7. 实现一种快速查找Richedit中可见区域内OLE对象的方法

    Richedit是一个OLE容器,使用Richedit来显示IM聊天内容时,通常使用OLE对象来实现在Richedit中播放表情动画. 触发表情的绘制有两种途径: 1.来自Richedit的刷新消息. ...

  8. Effective C++ 之 Item 1: 视C++为一个语言联邦

    Effective C++ Chapter 1. 让自己习惯C++(Accustoming Yourself to C++) Item 1. 视C++为一个语言联邦(View C++ as a fed ...

  9. 在Salesforce中对某一个Object添加 Validation Rule

    在Salesforce中可以对某一个Object添加相应的 Validation Rule 来进行一个全局的条件判断,比如满足什么样的条件的修改允许提交,不满足的要提示相应的错误信息. 要创建一个Va ...

  10. Android仿360手机卫士悬浮窗效果

    请看下图:                         首先是一个小的悬浮窗显示的是当前使用了百分之多少的内存,点击一下小悬浮窗,就会弹出一个大的悬浮窗,可以一键加速.好,我们现在就来模拟实现一下 ...