47. Permutations II (JAVA)
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)的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 47. Permutations II (Back-Track, Sort)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- tp 下载
public function download() { //文件名 $filename=input('filename'); // $file_dir = $_SERVER["DOCUME ...
- 身份证最后一位按照ISO7064:1983.MOD11-2校验码
居民身份证号码,根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地 ...
- npm-package-lock.json
npm notice created a lockfile as package-lock.json. You should commit this file. https://docs.npmjs. ...
- tomcat的work目录的作用
最近发现,很多网友喜欢把tomcat的work目录里的东西叫做缓存,其实那不是很恰当,work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录,这也正是为什么它 ...
- MySQL Online DDL工具
MySQL在线表结构变更工具 MySQL的大表表结构变更常用的解决方案无外乎三种: 一是利用Percona的pt-online-schema-change,Facebook的OSC等三方工具, 二是在 ...
- Windows下开发环境搭建
安装Make: https://sourceforge.net/projects/gnuwin32/ 安装G++:https://sourceforge.net/projects/mingw/
- 七、chromedriver各版本下载网址
http://chromedriver.storage.googleapis.com/index.html
- 四十、python中的生成器和迭代器
A.生成器(包含yield的就是生成器) def func(): print(11) yield 1 print(22) yield 2 print(33) yield 3 print(44) yie ...
- 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_5 Mybatis的CRUD-查询返回一行一列和占位符分析
聚合函数 模糊查询的另外一种写法 如果用户这种方式里面的value是固定的 因为在源码分析中,绑定的就是固定的value值 所以这里传参数的 没必要在用百分号了 删掉后 xml里面应该用这种方式来注释 ...
- oracle-参数文件的备份与还原
oracle-参数文件的备份与还原 参数文件是实例启动到nomount状态的必要条件,规定了实例的行为特征,位置跟操作系统相关,一般unix类的系统在$ORACLE_HOME/dbs目录下 (wind ...