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. How to use OpenChatter in my addon

    from:https://doc.openerp.com/trunk/mail/mail_openchatter_howto/ A small my_task model will be used a ...

  2. SQLite的升级(转)

    做Android应用,不可避免的会与SQLite打交道.随着应用的不断升级,原有的数据库结构可能已经不再适应新的功能,这时候,就需要对SQLite数据库的结构进行升级了. SQLite提供了ALTER ...

  3. MVC时间对比及时间范围判断

    方法一:使用DateTime.Compare 方法 public static int Compare( DateTime t1, DateTime t2 ) t1 早于 t2:小于零t1 与 t2  ...

  4. Java编程思想(十五) —— 类型信息之反射

    讲完.class,Class之后,继续. 1)泛化的Class引用 Class也能够增加泛型,增加之后会进行类型检查. 贴一下书上原话,Class<?>优于Class,尽管他们是等价的,C ...

  5. 新浪微博 使用OAuth2.0调用API

    # -*- coding: cp936 -*- #python 2.7.10 #xiaodeng #新浪微博 使用OAuth2.0调用API #微博开放接口的调用,都需要获取用户的身份认证.目前微博开 ...

  6. Nginx中Laravel的配置

    server { listen 80; server_name sub.domain.com; set $root_path '/var/www/html/application_name/publi ...

  7. IOS开发之瀑布流照片墙实现

    想必大家已经对互联网传统的照片布局方式司空见惯了,这种行列分明的布局虽然对用户来说简洁明了,但是长久的使用难免会产生审美疲劳.现在网上流行一种叫做“瀑布流”的照片布局样式,这种行与列参差不齐的状态着实 ...

  8. 浅析若干Java序列化工具【转】

    在Java中socket传输数据时,数据类型往往比较难选择.可能要考虑带宽.跨语言.版本的兼容等问题.比较常见的做法有: 采用java对象的序列化和反序列化 把对象包装成JSON字符串传输 Googl ...

  9. JMeter学习笔记--使用HTTP信息头管理器

    使用HTTP信息头管理,可以帮助测试人员设定JMeter发送的HTTP请求头所包含的信息.HTTP信息头中包含有”User-Agent".“Pragma".”Referer&quo ...

  10. PO_从PO追溯PR的方式和表(分析)

    2014-06-03 Created By BaoXinjian