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

SOLUTION 1:

还是经典的递归模板。需要处理的情况是:我们先把Num排序,然后只能连续地选,这样就可以避免生成重复的solution.
例子:1 2 3 4 4 4 5 6 7 8
444这个的选法只能:4, 44, 444连续这三种选法

我们用一个visit的数组来记录哪些是选过的。

 public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (num == null || num.length == 0) {
return ret;
} // For deal with the duplicate solution, we should sort it.
Arrays.sort(num);
boolean[] visit = new boolean[num.length]; dfs(num, new ArrayList<Integer>(), ret, visit); return ret;
} public void dfs(int[] num, ArrayList<Integer> path, List<List<Integer>> ret, boolean[] visit) {
int len = num.length;
if (path.size() == len) {
ret.add(new ArrayList<Integer>(path));
return;
} for (int i = 0; i < len; i++) {
// 只能连续地选,这样就可以避免生成重复的solution.
// 例子:1 2 3 4 4 4 5 6 7 8
// 444这个的选法只能:4, 44, 444连续这三种选法
if (visit[i] || (i != 0 && visit[i - 1] && num[i] == num[i - 1])) {
continue;
} // 递归以及回溯
visit[i] = true;
path.add(num[i]);
dfs(num, path, ret, visit);
path.remove(path.size() - 1);
visit[i] = false;
}
}
}

SOLUTION 2:

用一个pre来记录选过的值,也可以达到同样的目的,只取第一个可以取到的位置,后面再取也是一样的解。用pre记下来,后面就不要再取同样的值了

 // SOLUTION 2:
// 使用一个pre来记录。只取第一个可以取的位置
public void dfs(int[] num, ArrayList<Integer> path, List<List<Integer>> ret, boolean[] visit) {
int len = num.length;
if (path.size() == len) {
ret.add(new ArrayList<Integer>(path));
return;
} long pre = Long.MIN_VALUE;
for (int i = 0; i < len; i++) {
int n = num[i];
// 只取第一个可取的位置,因为别的位置取到的也没有区别
if (visit[i] || pre == n) {
continue;
}
pre = n; // 递归以及回溯
visit[i] = true;
path.add(n);
dfs(num, path, ret, visit);
path.remove(path.size() - 1);
visit[i] = false;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/permutation/PermuteUnique.java

LeetCode: Permutations II 解题报告的更多相关文章

  1. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  2. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  3. LeetCode: N-Queens II 解题报告

    N-Queens II (LEVEL 4 难度级别,最高级5) Follow up for N-Queens problem.

  4. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  5. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  6. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  7. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  9. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. 在js里双引号里又加单引号的解决方案常用WdatePicker

    EndTime: '<input name="EndTime" type="text" class="editable center decim ...

  2. eclipse 如何运行mavenWeb项目

      1.使用maven 首先,在eclipse中,使用maven对项目进行打包: 其次,将项目发布到Tomcat服务器上 说明: demo_WebService2-0.0.1-SNAPSHOT文件夹存 ...

  3. Redis学习(6)-常用命令

    List命令 value值为LinkedList类型. 使用环境: 1,做大数据集合的增删. 2,任务队列.用户任务队列 链表查看 lrange key start end:获取链表从start到en ...

  4. c# 获取北京时间更新本地计算机

    class UpdateDateTime { [DllImport("Kernel32.dll")] private static extern void SetLocalTime ...

  5. 12、static final

    Java中修饰常量用static final 用static final同时修饰的变量只能是成员变量而不能是局部变量 初始化: ①:定义时赋值 ②:静态代码块static{}中赋值 static 和 ...

  6. 【VUE+laravel5.4】vue给http请求 添加请求头数据

    1.适用于 ajax和普通的http请求 2.vue添加用法如下: <script type="text/javascript src="/dist/js/app.min.j ...

  7. 关于多线程中使用ArrayList的问题

    多线程全局变量:1.定义只读的全局变量时,必须加final修饰,即使是private的,防止被反射修改.2.对于需要多次读写的全局变量,一定要用ThreadLocal封装,避免多线程并发时变量被多次赋 ...

  8. Android之SurfaceView使用总结

    1.概念SurfaceView是View类的子类,可以直接从内存或者DMA等硬件接口取得图像数据,是个非常重要的绘图视图.它的特性是:可以在主线程之外的线程中向屏幕绘图上.这样可以避免画图任务繁重的时 ...

  9. 从使用 KVO 监听 readonly 属性说起

    01.KVO 原理 KVO 是 key-value observing 的简写,它的原理大致是: 1.当一个 object(对象) 有观察者时候,动态创建这个 object(对象) 的类的子类(以 N ...

  10. 工作总结 public DateTime? CollectionTime 可空类型 Code First

    数据库生成就对应生成  可以为空 的字段