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. iOS关于rar解压第三方库Unrar4iOS使用总结

    作者最近的公司项目要做实现rar解压的功能,在网上找了很久貌似关于rar解压的资料很少,不过有很多人推荐一个名叫“Unrar4iOS”的第三方开源框架,于是下载并尝试使用发现该开源框架并在使用过程中发 ...

  2. bzoj 3172 后缀数组|AC自动机

    后缀数组或者AC自动机都可以,模板题. /************************************************************** Problem: 3172 Us ...

  3. 【bzoj1061】 Noi2008—志愿者招募

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 (题目链接) 题意 给定n天,第i天需要ai个志愿者,有m类志愿者,每类志愿者工作时间为[l, ...

  4. POJ1737 Connected Graph

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  5. HDU 1060 Left-most Digit

    传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. App接口中json方式封装通信接口

    封装json通信接口的类 <?php class Response{ /** * 按json方式输出通信数据 * @param integer $code状态码 * @param string ...

  7. ECSHOP后台商品列表显示商品缩略图

    ECSHOP后台商品列表显示商品缩略图 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-06   ecshop 后台商品列表显示商品缩略图,大楷步凑如下: ...

  8. hdu 2045 不容易系列之(3)—— LELE的RPG难题

    解题思路: f(n)=1,2,.....n-2,n-1,n 前n-2个已经涂好,那么n-1有两种可能 1.n-1与n-2和1 的颜色都不同 1 粉,   n-2 红,   n-1 绿.  那么n的颜色 ...

  9. SQL触发器

    1. 创建一个触发器,当一本书被还回时,从LOAN表中删除相应的借阅记录,将该学生借阅这本书记录添加到LoadHist表中:并检查是否有用户在等待预约这本书,如有则将这本书的借阅状况修改为 已经预约: ...

  10. int(11)最大长度是多少,MySQL中varchar最大长度是多少(转)

    int(11)最大长度是多少,MySQL中varchar最大长度是多少? int(11)最大长度是多少? 在SQL语句中int代表你要创建字段的类型,int代表整型,11代表字段的长度. 这个11代表 ...