Permutations II 典型去重
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 典型去重的更多相关文章
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
随机推荐
- JS数组添加元素的三种方式
1.push() 结尾添加 数组.push(元素) 参数 描述 newelement1 必需.要添加到数组的第一个元素. newelement2 可选.要添加到数组的第二个元素. newelement ...
- C语言判断一个数能否被3和5整除
#include <stdio.h> /* 判断一个数能不能同时被3和5整除 --------soulsjie 20170525----- */ void main(){ int inpu ...
- hihoCoder#1069 最近公共祖先·三
原题地址 根据提示用Spase Table做 将Tree先展成List,因为数组长度等于边数的2倍,树中边数等于节点数-1,所以List数组只要开2倍节点数大小即可 WA了几次,原来是查询的时候出现左 ...
- codeforces Gym 100814 A、B、F、I
A题 先求出来这个数是第几大 阶乘求概率p 然后计算获得胜率的概率 常规解法把所有情况考虑一遍(跳1次,2次,3次……)要用到组合数 数可能太大了会爆的行不通 我们观察发现它有递推性质,从第二大 ...
- Core java for impatient 笔记 ch8 流
流stream 使用了数据视图,让你可以在比集合更高的概念上指定操作使用流,你只需要将操作的调度留给实现,例如,假设你要计算某个属性的平均值,你只需要指定数据源和属性,然后流类库会优化计算,比如使用多 ...
- Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用
一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...
- Ubuntu 16.04安装WinRAR/7-Zip(基于CrossOver)
基于CrossOver的WinRAR/7-Zip有如下缺点: 1.不能像Windows那样右键菜单解压 可以解决的问题: 1.可以使用提供的浏览工具进行文件选择再解压,只是在操作上多一步. 2.类似百 ...
- wget: unable to resolve host address “mirrors.163.com” 的解决办法
wget:无法解析主机地址.这就能看出是DNS解析的问题. 解决办法: 登入root(VPS). 进入/etc/resolv.conf. 修改内容为下nameserver 8.8.8.8 #googl ...
- Redis集群方案之主从复制(待实践)
Redis有主从复制的功能,一台主可以有多台从,从还可以有多台从,但是从只能有一个主.并且在从写入的数据不会复制到主. 配置 在Redis中,要实现主从复制架构非常简单,只需要在从数据库的配置文件中加 ...
- JDBC的异常
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/exceptions.html: 异常处理可以允许处理一个异常情况,例如可控方式的程序定义错误. 当异常 ...