[Leetcode] Permutations II
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的更多相关文章
- 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 @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- leetcode -- Permutations II TODO
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 排列
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. ...
随机推荐
- java 缩略图
http://www.cnblogs.com/digdeep/p/4829471.html http://www.jb51.net/article/57648.htm http://blog.csdn ...
- 重温WCF之会话Session(九)
转载地址:http://blog.csdn.net/tcjiaan/article/details/8281782 每个客户端在服务器上都有其的独立数据存储区,互不相干,就好像A和服务器在单独谈话一样 ...
- 几年前做家教写的C教程(之三专讲了递归和斐波那契)
C语言学习宝典(3) 数组: 一维数组的定义: 类型说明符 数组名[常量表达式] 例如: int a[10]; 说明:(1)数组名的命名规则和变量名相同,遵循标示符命名规则 (2)在定义数组时需要 ...
- 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)
算法训练 Torry的困惑(基本型) 时间限制:1.0s 内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...
- javascript中求浏览器窗口可视区域兼容性写法
1.浏览器窗口可视区域大小 1.1 对于IE9+.Chrome.Firefox.Opera 以及 Safari:• window.innerHeight - 浏览器窗口的内部高度• window. ...
- 跨平台C的IDE
1.JetBrains的新跨平台C++ IDE,CLion已经开始EAP了,不过这货是收费的 http://confluence.jetbrains.com/display/CLION/Early+A ...
- DTMF的原理分析
转自:http://blog.csdn.net/wangwenwen/article/details/8264925 1. DTMF原理 DTMF(Double Tone MulitiFrequenc ...
- PHPExcel设置数据格式的几种方法
转自:http://www.cnblogs.com/guangxiaoluo/archive/2013/11/19/3431846.html 解决 PHPExcel 长数字串显示为科学计数 在exce ...
- Android 插件化 动态升级
最新内容请见原文:Android 插件化 动态升级 不少朋友私信以及 Android开源交流几个 QQ 群 中都问到这个问题,这里简单介绍下 1.作用 大多数朋友开始接触这个问题是因为 App 爆棚了 ...
- 使用Genymotion安装APK出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE的解决办法
当我们安装好Genymotion后,把Android运用部署到上面调试时,console控制台会报错:Installation error: INSTALL_FAILED_CPU_ABI_INCOMP ...