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

解题思路一:

发现Java for LeetCode 046 Permutations自己想多了,代码直接拿来用,结果Time Limit Exceeded!看来还是不能直接用

解题思路二:

修改上述代码,去掉set,改用List,修改如下:

static public List<List<Integer>> permuteUnique(int[] nums) {
ArrayList<List<Integer>> set=new ArrayList<List<Integer>>();
dfs(set,nums,0);
return set;
}
static List<Integer> list=new ArrayList<Integer>();
static public void dfs(List<List<Integer>> set,int[] nums,int depth){
if(depth==nums.length){
set.add(new ArrayList<Integer>(list));
return;
}
for(int i=0;i<=depth;i++){
while(i<depth&&nums[depth]==list.get(i))
i++;
list.add(i,nums[depth]);
dfs(set,nums,depth+1);
list.remove(i);
}
}

结果还是Time Limit Exceeded!看来不排序直接DFS这条路是走不通了。

解题思路三:

既然不排序直接DFS走不通,因此,可以考虑排序后,然后以字典的方式进行全排列添加,考虑到在Java for LeetCode 031 Next Permutation题目中,我们已经写了一个字典的排列,稍作修改,添加boolean类型的返回值即可拿来用,JAVA实现如下:

static public List<List<Integer>> permuteUnique(int[] nums) {
ArrayList<List<Integer>> set=new ArrayList<List<Integer>>();
Arrays.sort(nums);
do{
List<Integer> list=new ArrayList<Integer>();
for(int num:nums)
list.add(num);
set.add(list);
}while(nextPermutation(nums));
return set;
}
static public boolean nextPermutation(int[] nums) {
int index = nums.length - 1;
while (index >= 1) {
if (nums[index] > nums[index - 1]) {
int swapNum=nums[index-1],swapIndex = index+1;
while (swapIndex <= nums.length - 1&& swapNum < nums[swapIndex])
swapIndex++;
nums[index-1]=nums[swapIndex-1];
nums[swapIndex-1]=swapNum;
reverse(nums,index);
return true;
}
index--;
}
reverse(nums,0);
return false;
}
static void reverse(int[] nums,int swapIndex){
int[] swap=new int[nums.length-swapIndex];
for(int i=0;i<swap.length;i++)
swap[i]=nums[nums.length-1-i];
for(int i=0;i<swap.length;i++)
nums[swapIndex+i]=swap[i];
}

结果Accepted!

Java for LeetCode 047 Permutations II的更多相关文章

  1. LeetCode 047 Permutations II

    题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...

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

  4. leetCode 47.Permutations II (排列组合II) 解题思路和方法

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

  5. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  6. [LeetCode] 47. Permutations II 全排列 II

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

  7. 【LeetCode】047. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. [LeetCode] 47. Permutations II 全排列之二

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

  9. LeetCode 47 Permutations II(全排列)

    题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description   给出数组,数组中的元素可能有重复,求出所有的全排列   使 ...

随机推荐

  1. structs环境搭建

    (1)<s:fielderror />放在JSP中,如果没在web.xml中配置filter相关内容,会有The Struts dispatcher cannot be found.从而显 ...

  2. 【bzoj3289】 Mato的文件管理

    http://www.lydsy.com/JudgeOnline/problem.php?id=3289 (题目链接) 题意 求区间逆序对 Solution 离线无修改查询,莫队转移:树状数组维护区间 ...

  3. 洛谷P2507 [SCOI2008]配对

    题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对. ...

  4. rqnoj71 拔河比赛

    题目描述 superwyh的学校要举行拔河比赛,为了在赛前锻炼大家,老师决定把班里所有人分为两拨,进行拔河因为为锻炼所以为了避免其中一方的实力过强老师决定以体重来划分队伍,尽 量保持两个队伍的体重差最 ...

  5. Codeforces 650B Image Preview

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  6. 人工神经网络(ANN)

    参考资料:http://www.cnblogs.com/subconscious/p/5058741.html 从函数上来看,神经网络是回归方程的级联叠加,用来逼近目标函数的,本质是一种模拟特征与目标 ...

  7. 【MVC5】ASP.NET MVC 项目笔记汇总

    ASP.NET MVC 5 + EntityFramework 6 + MySql 先写下列表,之后慢慢补上~ 对MySql数据库使用EntityFramework 使用域用户登录+记住我 画面多按钮 ...

  8. ewebeditor编辑器ASP/ASPX/PHP/JSP版本漏洞利用总结及解决方法

    这个编辑器按脚本分主要有4个版本,ASP/ASPX/PHP/JSP 每个版本都有可以利用的漏洞.判断网站是否使用了eWebEditor查看程序源代码,看看源码中是否存在类似”ewebeditor.as ...

  9. Javascript的调试利器:Firebug使用详解

    转载自:http://blog.csdn.net/tianxiaode/archive/2007/09/02/1769152.aspx   一直在用firebug,可是没有这么精通,今天看到本文章觉得 ...

  10. 把一个一维数组转换为in ()

    把一个一维数组转换为in()形式. function dbCreateIn($itemList) { if(empty($itemList )){ return " IN ('') &quo ...