https://leetcode.com/problems/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].

解题思路:

这题是 Permutations 的后续,不同之处在于,数组里可能有重复的数字。要求不能出现重复的排列,而不是排列里不能有重复的数字。

虽然这个类型的题目出现过很多次,但是这里去重的方法和前面还是有很大不同的。

首先,既然是排列,已经使用过的数字不能在当前排列中再次使用,所以要用一个数组visited来记录。

其次,如何避免题目里1,1,2这样的重复排列问题?当然首先要排序。

核心思想就是,在第i位上,上次已经选择过k,下次就不能再选k了。注意是同一个位置上的选择,而不是往下的递归,这个操作无法借助visited数组实现。如果像前面那样判断num[i]和num[i - 1]如果相等,就跳过呢?好像可以。可是遇到[1,1]这样的数组,因为num[1]==num[0],那么递归到第二步就跳出了,根本连一个排列都组成不了。如何做?

解决方法是,可以借助visited的数组。我们看到在上面的例子里,[1,1],如果num[i-1]已经被用过了,说明num[i]是本位置上第一次考虑的数字,无论他和num[i-1]是否相等,都是可以采用的。反之,如果num[i-1]还没被用过,说明num[i]是该位置上至少第二次考虑的元素了,并且前面考虑的就是num[i-1]。这时候,如果num[i]==num[i-1],就必须跳过此次选择。否则,等于一个位置上两次考虑一样的数字,肯定会有重复排列的。

代码如下:

public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
if(num.length == 0) {
return result;
}
Arrays.sort(num);
int[] visited = new int[num.length];
dfs(num, result, new LinkedList<Integer>(), visited);
return result;
} public void dfs(int[] num, List<List<Integer>> result, List<Integer> current, int[] visited) {
if(current.size() == num.length) {
result.add(new LinkedList(current));
return;
}
for(int i = 0; i < num.length; i++) {
if(visited[i] == 1) {
continue;
}
if(i > 0 && num[i] == num[i - 1] && visited[i - 1] == 1) {
continue;
}
visited[i] = 1;
current.add(num[i]);
dfs(num, result, current, visited);
current.remove(current.size() - 1);
visited[i] = 0;
}
}
}

总结一下排列组合的问题,去重的思路。

同一个排列或组合中,不能出现同一个位置上的数字,借助visited数组。

同一个排列或组合中,数组从小到大排列,必须对原数组排序,并且借助step,每次递归从step开始往后。

原数组有重复数字,同一个排列或组合中,允许出现重复数字,但是排列不允许重复,比较当前数字和前一个数字,如果相等则跳过。

Permutations II 典型去重的更多相关文章

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

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

  2. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  3. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

  4. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  5. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  6. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  7. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  8. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  9. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

随机推荐

  1. java生成6位随机数字

    //生成6位随机数字 System.out.println((int)((Math.random()*9+1)*100000)); //生成5位随机数字 System.out.println((int ...

  2. Leetcode 215.数组中的第k个最大元素

    数组中的第k个最大元素 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 ...

  3. 树剖 lca

    GeneralLiu  橙边为轻边 红边为重边 绿数为每个点的 top 橙数为每个点的编号 步骤 1 先预处理 每个点的 deep深度  size子树大小  dad父节点 2 再预处理 每个点的 to ...

  4. [TyvjP1050] 最长公共子序列(DP)

    传送门 f[i][j] 表示第 1 个串匹配到第 i 位,第 2 个串匹配到第 j 位的答案. f[i][j] = max(f[i - 1][j], f[i][j - 1])    (a[i] != ...

  5. 到达时间自动点击按钮弹出提示并跳转【JavaScript实现】

    原文发布时间为:2008-10-11 -- 来源于本人的百度文章 [由搬家工具导入] 其实我本来是想 做 在线考试的时候 规定时间到达时候自动交卷的,就想到这个例子了。。。。 代码: <html ...

  6. Linux下汇编语言学习笔记65 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  7. ***每天一个linux命令(5):rm 删除命令

    昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所 ...

  8. Spring在Java Filter注入Bean为Null的问题解决

    在Spring的自动注入中普通的POJO类都可以使用@Autowired进行自动注入,但是除了两类:Filter和Servlet无法使用自动注入属性.(因为这两个归Web容器管理)可以用init(集承 ...

  9. php-7.1编译记录

    编译php-7.1.28步骤 检查环境 ./configure \ --prefix=/u01/server/php-7.1.28 \ --enable-fpm \ --with-fpm-user=d ...

  10. SQLServer2008 快捷键集合

    CTRL       +       SHIFT       +       B                 生成解决方案           CTRL       +       F7     ...