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. c网购物车流程图

    1. 流程图 2. 流程介绍 1) 客人浏览模式下(未登录状态)加入购物车 这个时候回校验一下商品的可售数量,以及状态等等,校验成功后会保存到cookie和memcache,数据操作校验以memcac ...

  2. 获取某一个<tr>中<td>的值

    $("#trId").children("td").eq(0).text(};    //当前行的第一个<td>的值    <td>下标 ...

  3. .NET一般处理程序如何获取AJAX传递的参数

    POST的话 要用 HttpContext.Request.Form 和 HttpContext.Request.Params[""]   GET对应HttpContext.Req ...

  4. 学习使用windows下类似iptables的防火墙软件

    项目地址:http://wipfw.sourceforge.net一.下载地址:http://sourceforge.net/projects/wipfw/files/安装:解压软件包后执行insta ...

  5. 被老板逼着实现了Excle的透视表分析算法

    package com.example.demo; import java.sql.SQLException;import java.util.ArrayList;import java.util.H ...

  6. csu - 1537: Miscalculation (模拟题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1537 因为给出的式子是必定合法的,只要用两个栈分别保存符号和数字.算出答案后和从左至右算的答案比对 ...

  7. Codeforces 628F Bear and Fair Set

    题意: 给定若干个上限upto以及集合中在[1,upto]中的元素个数,问是否存在这样的集合使得集合中的元素除以5的余数的个数相等. 分析: 首先可以想到区间的数与其除以5的余数和区间编号分别一一对应 ...

  8. webstrom设置代码缩进四个空格

    言,今天在github上拉了一份代码,发现代码缩进是2个空格,不习惯.找到了解决方案. 一.File ---> Settings 搜索Code Style,取消勾选使用现文件的缩进方式,见下图. ...

  9. css3自定义流动条

    <style> .item { height: 180px; overflow: auto; width: 180px; float: left; margin: 11px; box-sh ...

  10. how to read openstack code : routes

    When coding a web system, you have to think about an important problem, how to map urls to logic. Op ...