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. [
  2. [1,1,2],
  3. [1,2,1],
  4. [2,1,1]
  5. ]
  6.  
  7. 和一般的Permutation不一样的是,这种permutation需要排序,使相同的元素能够相邻,选取下一个元素的时候,要查看这个元素的前一个元素是否和它相同,如果相同而且没有使用过,就不用选取这个元素,因为如果选取了这个元素,所得的结果被包含于选取了前一个相同元素的结果中。
    代码如下:
  1. public class Solution {
  2. int length;
  3. public List<List<Integer>> permuteUnique(int[] nums) {
  4. length = nums.length;
  5. List<List<Integer>> result = new ArrayList<List<Integer>>();
  6. if (length == 0) {
  7. return result;
  8. }
  9. Arrays.sort(nums);
  10. boolean[] flags = new boolean[length];
  11. Arrays.fill(flags, false);
  12. List<Integer> candidate = new ArrayList<Integer>();
  13. helper(nums, length, flags, result, candidate);
  14. return result;
  15. }
  16. private void helper(int[] nums, int n, boolean[] flags, List<List<Integer>> result, List<Integer> candidate) {
  17. if (n == 0) {
  18. result.add(new ArrayList<Integer>(candidate));
  19. }
  20. int ll = candidate.size();
  21. for (int i = 0; i < length; i++) {
  22. if (!flags[i]) {
  23. //if the number appears more than once and the same number before it has not been used
  24. if (i != 0 && nums[i] == nums[i - 1] && !flags[i - 1]) {
  25. continue;
  26. }
  27. flags[i] = true;
  28. candidate.add(nums[i]);
  29. helper(nums, n - 1, flags, result, candidate);
  30. candidate.remove(ll);
  31. flags[i] = false;
  32. }
  33. }
  34. }
  35. }

[leetcode] 47. Permutations II的更多相关文章

  1. leetCode 47.Permutations II (排列组合II) 解题思路和方法

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

  2. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  4. LeetCode 47 Permutations II(全排列)

    题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description   给出数组,数组中的元素可能有重复,求出所有的全排列   使 ...

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

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

  6. 【LeetCode】47. Permutations II

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

  7. [LeetCode] 47. 全排列 II

    题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...

  8. 【leetcode】Permutations II

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

  9. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. ED2k Resource

    http://www.lwkk.com/ http://www.ed2000.com/

  2. linux下重启apache

    基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/local/apache2/bin/apachec ...

  3. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  4. 【Beta】七天屠蛟记

    团队名字: 一不小心就火了 屠龙天团少年们: 031402504 陈逸超 (组长) 031402505 陈少铭 031402511 黄家俊 031402515 翁祖航 031402516 黄瑞钰 03 ...

  5. Charles抓Android的数据包

    1. 获得Mac OS的IP地址 2. 对Android手机设置代理,主机IP是步骤1中获得的IP,端口8888

  6. Windows XP系统下添加任务计划常出现问题解决办法

    Windows XP系统下添加任务计划常出现问题解决办法 计划任务就是让电脑在指定的时间内执行指定的动作(计划动作),这些动作可以是一个程序,也可以是一个批处理,但是至少是可以运行的(通俗一些就是双击 ...

  7. ActiveMQ启动多个broker

    具体步骤如下: 1.把activemq目录下的conf文件复制一份,叫做conf2, 命令: cp -r conf conf2 2.修改conf2目录下的activemq.xml文件 a.修改brok ...

  8. C# 读取excel日期时获取到数字转换成日期

    string strDate= DateTime.FromOADate(Convert.ToInt32(data[i][7])).ToString("d"); strDate= D ...

  9. 大熊君学习html5系列之------History API(SPA单页应用的必备------重构完结版)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...

  10. tyvj1194 划分大理石

    描述 有价值分别为1..6的大理石各a[1..6]块,现要将它们分成两部分,使得两部分价值之和相等,问是否可以实现.其中大理石的总数不超过20000.  输入格式 有多组数据!所以可能有多行如果有0  ...