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. js阻止表单重复提交

    //校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); a ...

  2. BZOJ2463 谁能赢呢?

    Description   小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的 ...

  3. [转] 计算几何模板Orz

    #include<math.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define PI acos(-1.0) ...

  4. UVa247 Calling Circles

    Time Limit: 3000MS     64bit IO Format: %lld & %llu map存人名,floyd传递闭包,DFS查询. 输出答案的逗号后面还有个空格,被坑到了2 ...

  5. 什么是领域模型(domain model)?贫血模型(anaemic domain model) 和充血模型(rich domain model)有什么区别

    http://blog.csdn.net/helloboat/article/details/51208128 领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专 ...

  6. c# 闭包 小例

    class Program { static void fnnn() { string[] k = new string[] { "x", "y", " ...

  7. 将Spark中CompactBuf转换为String

    val rdd = sc.textFile("hdfs://hbase11:9000/sparkTsData/ipsoftware/wincc").map{ line => ...

  8. 初识A*算法

    写这篇文章的初衷是应一个网友的要求,当然我也发现现在有关人工智能的中文站点实在太少,我在这里抛砖引玉,希望大家都来热心的参与. 还是说正题,我先拿A*算法开刀,是因为A*在游戏中有它很典型的用法,是人 ...

  9. 复选框全选、全不选和反选的效果实现VIEW:1592

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. jquery基础总结

    什么是jQuery?        就是一个JavaScript函数库,开源的.jQuery能做什么        JavaScript是做什么的,jQuery就是做什么的,Jquery是对javas ...