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 ...
随机推荐
- QT信号槽 中的对象野指针
例: connect(&objec1,&class::slot_func1,&object2,&class::slot_func2) 如果 &objec 传 ...
- Python3学习笔记(八):集合
集合(set)是一种可变的无序的不重复的数据类型 要创建集合,需要将所有项(元素)放在花括号({})内,以逗号(,)分隔. >>> s = {'p','y','t','h','o', ...
- C#中的事件委托
C#中的事件与委托,对于我们写业务代码的程序员来说不常用,这就会导致经常忘记,这边再温习一下. //委托 public delegate void MyEventDelegateHandler(str ...
- Comparable接口与Comparator接口的比较————Comparator接口详解
Comparator接口位于:java.util包中. Comparator接口:1. 强行对某个对象的Collection进行整体排序.值得注意的是:Comparator接口可以作为参数传到一些so ...
- 20165218 《网络对抗技术》 Exp9 网络安全基础
Exp9 网络完全基础 基础问题回答 SQL注入攻击原理,如何防御 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. ...
- class 用法 函数变量的作用域
函数变量的作用域 1. 函数体内声明的变量 2. 参数中的变量 没有赋值的 function fn(a){} 赋值的,值不是变量 function fn(a=45){} 赋的值为变量 function ...
- centos设置定时删除文件定时清理网站日志
1.进入linux系统 2.在任意目录创建一个sh后缀的文件,如: 3.编辑打开该文件,如图: 4.此时按键盘上的“i”键或者“insert”键,进入编辑模式 输入: #!/bin/shfind /d ...
- centos6.5+jdk1.7+mysql5.6+tomcat8.0部署jpress
前言:此篇记录在linux下搭建环境部署jpress,mysql使用的是源码安装 1.准备 2.安装 3.部署 1.准备 a.准备centos6.5服务器环境 mysql-5.6.19.tar.gz ...
- 使用cesium中的scene.open中遇到的几个问题
有些服务是发在场景(scene)下的,超图提供了一个很方便的方法:scene.open,这个方法会将场景中所有的图层(无论是OSGB还是影像和地形)加载进来.同时这个方法会自带一个自动地位功能,具体实 ...
- 机器学习【一】K最近邻算法
K最近邻算法 KNN 基本原理 离哪个类近,就属于该类 [例如:与下方新元素距离最近的三个点中,2个深色,所以新元素分类为深色] K的含义就是最近邻的个数.在sklearn中,KNN的K值是通过n ...