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].

题目大意:给一个数组,包含重复元素,返回所有可能组合,去重。

解题思路:这题是Permutation的变种,思路就是回溯。首先把数组排序一下,对于不是第一次使用的数字,检测是否出现过。

    public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
List<Integer> tmp = new ArrayList<>();
boolean[] used = new boolean[nums.length];
helper(res, tmp, 0, nums.length, nums, used);
return res;
} public void helper(List<List<Integer>> res, List<Integer> tmp, int n, int N, int[] nums, boolean[] used) {
if (n == N) {
res.add(new ArrayList<>(tmp));
return;
}
int last_num = 0x80000000;
for (int i = 0; i < N; i++) {
if (!used[i] && last_num != nums[i]) {
used[i] = true;
tmp.add(nums[i]);
last_num = nums[i];
helper(res, tmp, n + 1, N, nums, used);
used[i] = false;
tmp.remove(tmp.size() - 1);
}
}
}

Permutations II ——LeetCode的更多相关文章

  1. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  2. Permutations II leetcode java

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  3. leetcode Permutations II 无重全排列

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

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

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

  5. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

  6. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  7. 【leetcode】Permutations II

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

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

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

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

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

随机推荐

  1. 开通博客第一天 (先发一些android(java)常见异常信息

    常见异常: java.lang.AbstractMethodError抽象方法错误.当应用试图调用抽象方法时抛出. java.lang.AssertionError断言错.用来指示一个断言失败的情况. ...

  2. CentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)

    原文链接:http://www.jb51.net/article/29668.htm

  3. iOS远程消息推送自我整理版

    @interface AppDelegate () <UIApplicationDelegate> @end @implementation AppDelegate - (BOOL)app ...

  4. 静态方法块 static 以及对象属性&类属性的用法

    使用静态块的好处:只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用.如果不使用它,就会出现如下问题:new一个对象,我就要调用一次所需的这些内容,重复 ...

  5. Sqoop import加载HBase过程中,遇到Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-x

    在执行hbase sqoop抽取的时候,遇到了一个错误,如下图: 在执行程序的过程中,遇到权限问题很正常,也容易让人防不胜防,有问题就想办法解决,这个是关键. 解决办法如下: 第一步:su hdfs, ...

  6. 再看IOC, 读深入理解DIP、IoC、DI以及IoC容器

    IoC则是一种 软件设计模式,它告诉你应该如何做,来解除相互依赖模块的耦合.控制反转(IoC),它为相互依赖的组件提供抽象,将依赖(低层模块)对象的获得交给第三方(系统)来控制,即依赖对象不在被依赖模 ...

  7. 第五篇、 WebSphere8.5的安装

    一.前言 WebSphere Application  Server 是IBM企业级应用服务器,与WAS6,WAS7相比较而言 WAS8发生了很大的改变,其安装介质和以前截然不同,该篇章中对于不同的安 ...

  8. grunt live

    { "name": "grunt-live-test", "version": "0.1.0", "autho ...

  9. 基于jQuery 的图片瀑布流实现

    解题思路: 第1步  分析问题:我这边的处理方式是以列为单位.每次滚动条滚到底部,把需要加的新的内容放在高度最小的列.如下图所示 加载后的显示 如果在继续往下滚动.新图片就会在1下边显示,如此类推. ...

  10. curl http_code状态码 含义

    curl爬取过程中,会返回一个http_code,下面是他们的意义信息 $http_code["]="Unable to access"; $http_code[&quo ...