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

Example:

Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

有重复数字的情况,之前在Subsets II,我们采取的是在某一个递归内,用for循环处理所有重复数字。这里也相同,需要在递归内考虑重复数字,重复数字只能插入在已插入的重复数字之前,碰到相同的数字,即停止循环,退出递归。

class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<Integer> ans = new ArrayList<Integer>();
if(nums.length == 0) return ret; ans.add(nums[0]);
insertNum(nums, 1, ans);
return ret;
} public void insertNum(int[] nums, int index, List<Integer> ans){
if(index == nums.length) {
List<Integer> new_ans = new ArrayList<Integer>(ans);
ret.add(new_ans);
return;
} for(int j = 0; j < ans.size(); j++){ //iterate all possible insert position
ans.add(j,nums[index]);
insertNum(nums, index+1, ans);
ans.remove(j); //recover if(ans.get(j)==nums[index] ) return; //avoid repeat, 重复的数字只能添加在已有数字之前
}
//insert in the back
ans.add(nums[index]);
insertNum(nums, index+1, ans);
ans.remove(ans.size()-1); //recover
} private List<List<Integer>> ret = new ArrayList<List<Integer>>();
}

47. Permutations II (JAVA)的更多相关文章

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

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

  2. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  3. 【LeetCode】47. Permutations II

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

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

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

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

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

  6. 47. Permutations II

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

  7. 47. Permutations II (Back-Track, Sort)

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

  8. [leetcode] 47. Permutations II

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

  9. LeetCode 【47. Permutations II】

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

随机推荐

  1. windows 安装 Mongodb 数据库及操作图形化软件 Robo 3T

    1 下载系统对应的正确 Mongodb 和 Robo 3T 版本 2 选中 Mongodb 需要安装的路径(后续会使用路径) 3 启动 Mongodb 服务器(到安装相关的路径) 可以参考 菜鸟教程 ...

  2. Android网络技术之WebView常用方法

    public class WebViewTest extends Activity {       private WebView wv;     private EditText et;       ...

  3. Android中StatFs获取系统/sdcard存储(剩余空间)大小

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. 利用spark将表中数据拆分

    i# coding:utf-8from pyspark.sql import SparkSession import os if __name__ == '__main__': os.environ[ ...

  5. socket通信(TCP和UDP)

    1.TCP 2.UDP

  6. window.open传递多个参数

    在前台使用var url = 'AddFiles.aspx?name=' + nm + '&id=' + id; window.open(url, "", "wi ...

  7. JSON.stringify方法报错:Converting circular structure to JSON

    别以为JSON.parse(JSON.stringify(data))做深拷贝无敌,对于以下这种情况,当你需要保留父级对象,即 对象存在循环引用,就会报错. var a = [ { "id& ...

  8. leetcode-easy-array-1 two sum

    mycode  33.91% class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i ...

  9. Failed building wheel for netifaces

    目录 文章目录 目录 问题 解决 问题 安装 OpenStackClient 的时候发现问题: Failed building wheel for netifaces Running setup.py ...

  10. [转]delphi 防止刷新时闪烁的终极解决办法

    { 防止刷新时闪烁的终极解决办法(对付双缓冲无效时) }Perform($000B, 0, 0); //锁屏幕 防止闪烁 // 做一些会发生严重闪烁的事情.. //解锁屏幕并重画Perform($00 ...