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. 【Codeforces 231C】To Add or Not to Add

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 把数组排个序, 显然优先用大的且小于枚举的数字a[i]的数字变成a[i] 那么肯定有一个范围j. 然后a[j~i-1]都能在k花费以内变成a[ ...

  2. readdir() 获取文件类型

    readdir()获取文件类型 //// 字符设备文件 type =2, filename207=tty0 crw-rw----  1 root root     4,  0 04-10 16:28 ...

  3. Android GIS开发系列-- 入门季(8) Json与Geometry的相互转换

    在Android中json数据十分普遍,也很实用,在Arcgis中也同样支持Json数据,Json与Geometry可以相互转换,达到我们想要的数据. 一.Geometry转换成Json数据 这个实现 ...

  4. Android Jni Android.mk经常使用语句

    仅仅要涉及JNI开发都涉及到Android.mk编写,它也是一种makefile语言. 以上一篇博客中提供的project为例! <1> : 信息打印 : 既然是一种简易语言那么首先应该知 ...

  5. ORA-00942:表或视图不存在 低级错误一例

    ORA-00942:表或视图不存在  低级错误一例 运行查询语句,报ORA-00942错误 检查后发现没有指定表的所属用户.加入用户.再次查询,查询正常,截图例如以下: *************** ...

  6. js类继承扩展super

    相应的资料https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/super 例子: class Pol ...

  7. vue全局注册与局部注册的写法

    vue全局注册是每个实例化的vue都可以使用,而局部则是实例化注册的那个可以用.举个例子,看看写法: <div id="app"> <p>页面载入时,inp ...

  8. Akka并发编程——第五节:Actor模型(四)

    本节主要内容: 1. 停止Actor 1. 停止Actor (1)通过ActorSystem.shutdown方法停止全部 Actor的执行 /* *停止Actor:ActorSystem.shutd ...

  9. Spring——IoC

    控制反转(Inversion ofControl,英文缩写为IoC)是一种能够解耦的方法,不是什么技术.是一种思想,也是轻量级的Spring框架的核心.控制反转一般分为两种类型.依赖注入(Depend ...

  10. Vim i和a差别

    i是当前位置插入 a是当前文字的后面插入