Permutations 排序(有重复数)II @LeetCode
来源:互联网 发布:知来者之可追什么意思 编辑:程序博客网 时间:2024/04/30 06:46
比起http://blog.csdn.net/fightforyourdream/article/details/14217105
多加了一个while来去重,发现这个去重方法在另一道题也用过,同样也是DFS里面去重,很好用!
另外就是在最前面加了一个sort,因为如果没加,当输入乱序时就会Output Limit Exceed!
package Level4;import java.util.ArrayList;import java.util.Collections;/** * 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]. * */public class S47 {public static void main(String[] args) {int[] num = {1,2,1};System.out.println(permuteUnique(num));}public static ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();ArrayList<Integer> done = new ArrayList<Integer>();ArrayList<Integer> rest = new ArrayList<Integer>();for(int val : num){rest.add(val);}Collections.sort(rest);// 这里要先排序一下。。否则遇到非递增排序的输入就会Output Limit Exceed!rec2(done, rest, ret);return ret;}public static void rec2(ArrayList<Integer> done, ArrayList<Integer> rest, ArrayList<ArrayList<Integer>> ret){if(rest.size() == 0){ret.add(new ArrayList<Integer>(done));return;}for(int i=0; i<rest.size(); i++){done.add(rest.get(i));ArrayList<Integer> newRest = new ArrayList<Integer>(rest);newRest.remove(i);rec2(done, newRest, ret);done.remove(done.size()-1);while(i<rest.size()-1 && rest.get(i)==rest.get(i+1)){i++;}}}}
过滤重复元素方法同 http://blog.csdn.net/fightforyourdream/article/details/16859111
public class Solution { public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> done = new ArrayList<Integer>(); ArrayList<Integer> rest = new ArrayList<Integer>(); Arrays.sort(num); for(int val : num){ rest.add(val); } rec(done, rest, ret); return ret; } public void rec(ArrayList<Integer> done, ArrayList<Integer> rest, ArrayList<ArrayList<Integer>> ret){ if(rest.size()==0){ ret.add(new ArrayList<Integer>(done)); return; } for(int i=0; i<rest.size(); i++){ int first = rest.remove(i); done.add(first); rec(done, rest, ret); done.remove(done.size()-1); rest.add(i, first); // insert to its original position while(i+1<rest.size() && rest.get(i)==rest.get(i+1)){ i++; } } }}
1 0
- Permutations 排序(有重复数)II @LeetCode
- [LeetCode]—Permutations II 求全排列(有重复值)
- Permutations II 有重复的排序
- leetcode-47. Permutations II(重复元素全排列)
- Permutations II:有重复元素的排列
- leetcode系列(55)Permutations, Permutations II
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode(47)Permutations II
- 求全排列,不重复(Permutations II)
- leetcode:Permutations II (序列所有无重复排列)【面试算法题】
- leetcode 47. Permutations II 全排列问题(去掉重复元素)+递归
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- LeetCode 题解(107): Permutations II
- Windows下mod_python + Apache 配置笔记
- IP分组的交付和转发 : (5) 路由器的构件
- ASCII码表
- HHVM搭建胜过ZendPHP五六倍的PHPWeb服务器(高性能PHP虚拟机Hiphop)
- 我的第一篇博客《浅谈SSH框架原理,并模仿实现Spring中Aop框架》
- Permutations 排序(有重复数)II @LeetCode
- LINQ分页和排序,skip和Take 用法
- FOR UPDATE、FOR UPDATE NOWAIT、WAIT详解
- Tomcat7.0管理用户配置
- 爱是生命的火焰
- Linux下which、whereis、locate、find 命令查找文件
- 三星Note3手势翻页/浮窗浏览使用技巧
- cocos2d-x 有关CCEditBox设置除输入框以外的可点击触发区域
- nginx location 路径规则