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. TCP连接的11种状态,三次握手四次挥手原因

    1).LISTEN:首先服务端需要打开一个socket进行监听,状态为LISTEN. /* The socket is listening for incoming connections. 侦听来自 ...

  2. Vim 命令、操作、快捷键(收藏大全)

    ------ 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filenam ...

  3. SQL Server服务起不了

    转载 MSSQLSERVER服务无法启动的解决方案   1.IP地址配置不正确: 打开 Microsoft SQL Server 2005配置工具下的SQL Server Configuration ...

  4. es之主分片和复制分片的交互过程

    1:索引(创建或者删除)一个文档 首先:发送一个索引或者删除的请求给node1 其次:node1接收到请求之后,会根据请求中携带的参数“文档id”判断出该文档应该存储在具体哪一个shard中 shar ...

  5. sqli-labs(9)

    基于时间的GET单引号盲注 0x01爱之初试探 在我们注入了SQL代码之后,存在以下两种情况: 如果注入的SQL代码不影响后台[数据库]的正常功能执行,那么Web应用的页面显示正确(原始页面). 如果 ...

  6. windows安装程序制作

    作为一个学计算机的,现在才知道那些安装软件都是用软件封装工具封装起来的. 我们写好exe以后可以下载一个Inno setup5 对其打包成可安装的软件,期间可加入图标,readme,等等一些东西.

  7. 写入mongodb

    https://blog.csdn.net/u013421629/article/details/78885079 https://www.jianshu.com/p/7d14c3ad810f  可视 ...

  8. 【mysql】错误代码1308 Invalid use of NULL value

    错误原因是: 在最初设计表script_run_detail表时,resut_id忘记勾选不是null选项, 导致存储数据后发现result_id有NULL值,而实际上,我不希望这个字段可以存储NUL ...

  9. JavaScript对象的常用属性及使用

    什么是浏览器对象模型? 浏览器对象模型(BOM Browser Object Model)是JavaScript的组成之一,它提供了独立于内容和浏览器窗口进行交互的对象,使用浏览器对象模型可以实现与H ...

  10. Unity—Compoent类

    官方API->Componment   新引入成员 作用 字段 gameobject 该组件所在的游戏对象 tag 游戏对象的标签 Transform 添加在游戏对象上的transform组件 ...