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 ...
随机推荐
- 配置 Hive On Tez
配置 Hive On Tez 标签(空格分隔): hive Tez 部署底层应用 简单介绍 介绍:tez 是基于hive 之上,可以将sql翻译解析成DAG计算的引擎.基于DAG 与mr 架构本身的优 ...
- 【Django】搭建Django administration并登录
Python自带一个后台管理系统,这个后台管理系统搭建与登陆呢? 新建项目Django_Admin 文件结构目录如下: 创建APP 在pycharm下方的terminal终端中输入命令: python ...
- 梯度、Hessian矩阵、平面方程的法线以及函数导数的含义
本文转载自: Xianling Mao的专栏 =========================================================================== 想 ...
- leetcode 234 回文链表 Palindrome Linked List
要求用O(n)时间,和O(1)空间,因此思路是用本身链表进行判断,既然考虑回文,本方法思想是先遍历一次求链表长度,然后翻转前半部分链表:然后同时对前半部分链表和后半部分链表遍历,来判断对应节点的值是否 ...
- 2019 ccpc 秦皇岛
D 如果1/n是有限小数,不停乘以10,一定在有限次之后成为一个整数. 10的质因子只有2和5,只要保证分母的质因子只有2和5即可 #include <iostream> #include ...
- 使用Callable或DeferredResult实现springmvc的异步请求
使用Callable实现springmvc的异步请求 如果一个请求中的某些操作耗时很长,会一直占用线程.这样的请求多了,可能造成线程池被占满,新请求无法执行的情况.这时,可以考虑使用异步请求,即主线程 ...
- Octavia 项目加速 OpenStack LBaaS 落地大规模应用场景
目录 文章目录 目录 OpenStack LBaaS Octavia 软件架构 网络架构 操作对象基本概念 功能实现基本概念 Ocatvia Daemon 列表 部署 Ocatvia 手动方式集成 O ...
- 测开之路一百三十八:会话管理之session
session管理和使用,需要用到flask的session模块和设置安全码:app.secret_key 比如列表页和编辑功能只能给admin用 列表页 编辑页 添加session 登录成功时,把u ...
- oracle data guard --理论知识回顾01
之前搭建了rac到单实例的dg环境,最近又在windows下搭建了dg,这一篇关于dg的一些理论知识回顾 官方文档 https://docs.oracle.com/cd/E11882_01/nav/p ...
- Java的in.nextInt()和in.nextLine()方法的具体内涵
本人也是刚开始学习java语言,在学习的过程中,老师让我们做一个模拟学生学籍管理系统的小程序.因为刚开始,做的是比较简单的,用switch语句做界面,然后配合Scanner 接收输入的数字进行跳转,完 ...