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

难度:80+15(怎样跳过重复的case来节省时间)。我一开始的想法很简单,就是在permutation这道题的基础上,在把合乎条件的permutation加入permutations的时候,加入一个查重语句。如果是重复的,就不添加。之前Subset II 问题我就是这么做的。

1         if (permutation.size() == num.length) {
2             if (!permutations.contains(permutation)) {
3                 permutations.add(new ArrayList<Integer>(permutation));
4                 return;
5             }
6         }    

可是这一次行不通了,报TLE。我就开始想,怎么节省时间呢?网上参看了别人的想法,说是对于重复的元素循环时跳过递归函数的调用。对题目这个例子来说就是,如果permutation已经计算过了[num[0], num[1], num[2]]即[1,1,2]这个case,那么之后如果permutation计算到[num[1], num[0], num[2]]又是[1,1,2]这个case的时候,需要跳过,不用算了。这样就可以剩下这部分递归的时间。

怎么知道当前的这个permutation之前出现过没有呢?(本题重点)

首先我们要对元素集合排序,从而让重复元素相邻,接下来就是一行代码对于重复元素和前面元素使用情况的判断。如果第一个重复元素前面的元素还没在当前结果中,那么我们不需要进行递归。

 1 public class Solution {
 2     public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
 3         Arrays.sort(num);
 4         ArrayList<Integer> permutation = new ArrayList<Integer>();
 5         ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
 6         boolean[] visited = new boolean[num.length];
 7         helper(permutation, permutations, num, visited);
 8         return permutations;
 9     }
10
11     public void helper(ArrayList<Integer> permutation, ArrayList<ArrayList<Integer>> permutations, int[] num, boolean[] visited) {
12         if (permutation.size() == num.length) {
13                 permutations.add(new ArrayList<Integer>(permutation));
14                 return;
15         }
16
17         for (int k = 0; k < num.length; k++) {
18             if (k > 0 && !visited[k-1] && num[k] == num[k-1]) continue;
19             if (!visited[k]) {
20                 visited[k] = true;
21                 permutation.add(num[k]);
22                 helper(permutation, permutations, num, visited);
23                 permutation.remove(permutation.size() - 1);
24                 visited[k] = false;
25             }
26         }
27     }
28 }

Leetcode: Permutations II相关推荐

  1. [LeetCode] Permutations II 全排列之二

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

  2. LeetCode Permutations II(有重复元素的全排列)

    问题:给出一个有重复元素的数组,要求输出全排列 思路:先排序,然后使用next_permutation 具体代码参考: https://github.com/wuli2496/OJ/tree/mast ...

  3. LeetCode:Permutations II

    Problems: Given a collection of numbers that might contain duplicates, return all possible unique pe ...

  4. [LeetCode] Permutations II

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

  5. Leetcode - Permutations I,II

    Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...

  6. 【数字全排列】LeetCode 47. Permutations II

    LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...

  7. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example,  [1,2,3]  ...

  8. LeetCode Permutations

    原题链接在这里:https://leetcode.com/problems/permutations/ 题目: Given a collection of distinct numbers, retu ...

  9. LeetCode Permutations(不重复数的所有排列)

    问题:给出一个没有重复的数组,要求输出所有的排列 思路:先排序,然后使用STL中的next_permution 具体参考: https://github.com/wuli2496/OJ/tree/ma ...

最新文章

  1. python datetime模块
  2. 怎么这一个c语言的dll文件,如何在C中获取DLL文件的版本信息
  3. [Android Pro] AndroidX重构和映射
  4. CentOS 6.5使用yum快速搭建LAMP环境
  5. linux下查看监听端口对应的进程
  6. TorgoiseGit配置ssh密钥
  7. java带参数的构造方法_崤云说 | JAVA面向对象
  8. 只能匹配第一列吗_VLOOKUP会用了吗?不会的抓紧看
  9. 程序人生:什么是“对用户友好”
  10. hdu 1317——XYZZY
  11. 功能至上!国内外最实用的协作类软件盘点
  12. layui table异步调用数据的时候,数据展示不出来现象解决方案
  13. android开发那些事儿(一)
  14. 9月份红帽认证考试又 PASS 19位同学
  15. 空间索引-四叉树的实现及其应用
  16. 汽车电子中的2520贴片晶振
  17. 怎么安装光盘并重新启动计算机,如何用光盘重新安装电脑系统
  18. 干货丨Kubernetes 中分析调试网络流量的4种方法
  19. 手机移动端WEB资源整合
  20. 手上长有透明液体的小疙瘩并脱皮怎么办?

热门文章

  1. DatePicker的使用(一)
  2. Activity的用法(一):Notification Activity
  3. Kafka-manager启动命令
  4. win10 ObservableCollection 排序自动收缩问题
  5. 我在清华当工程师的日子
  6. 一种关于感受野尺寸计算的思路
  7. 【jQuery】选择器
  8. Android成长日记-使用ViewFlipper实现屏幕切换动画效果
  9. Node.js的核心与红利
  10. How to go between HK and Shenzhen (Futian)?