[leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2] have the following unique permutations:
[
[1,1,2],
[1,2,1],
[2,1,1]
] 和一般的Permutation不一样的是,这种permutation需要排序,使相同的元素能够相邻,选取下一个元素的时候,要查看这个元素的前一个元素是否和它相同,如果相同而且没有使用过,就不用选取这个元素,因为如果选取了这个元素,所得的结果被包含于选取了前一个相同元素的结果中。
代码如下:
public class Solution {
int length;
public List<List<Integer>> permuteUnique(int[] nums) {
length = nums.length;
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (length == 0) {
return result;
}
Arrays.sort(nums);
boolean[] flags = new boolean[length];
Arrays.fill(flags, false);
List<Integer> candidate = new ArrayList<Integer>();
helper(nums, length, flags, result, candidate);
return result;
}
private void helper(int[] nums, int n, boolean[] flags, List<List<Integer>> result, List<Integer> candidate) {
if (n == 0) {
result.add(new ArrayList<Integer>(candidate));
}
int ll = candidate.size();
for (int i = 0; i < length; i++) {
if (!flags[i]) {
//if the number appears more than once and the same number before it has not been used
if (i != 0 && nums[i] == nums[i - 1] && !flags[i - 1]) {
continue;
}
flags[i] = true;
candidate.add(nums[i]);
helper(nums, n - 1, flags, result, candidate);
candidate.remove(ll);
flags[i] = false;
}
}
}
}
[leetcode] 47. Permutations II的更多相关文章
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列 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 ...
- LeetCode 47 Permutations II(全排列)
题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. 全排列 II
题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- mysql中now()函数的使用,还有oracle的sysdate,可能埋下的坑
mysql中now()函数的使用,还有oracle的sysdate 在需求中如果系统中药添加当前操作的时间那么很简单的一个操作在写sql的时候直接在这个字段对应的位置写上now()函数就可以了,这样就 ...
- 消息中间件:RabbitMQ基本探索
RabbitMQ是一个基于AMQP协议(Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中 ...
- Python Day6
面向对象 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强...&qu ...
- Recall, Precision and F-score
F1 score (also F-score or F-measure) ,调和平均数稍微有点不好理解,最关键的是,不知道分子的情况下,采用调和平均数.
- 【迁移】—Entity Framework实例详解 转
一.Entity Framework 迁移命令(get-help EntityFramework) Enable-Migrations 启用迁移 Add-Migration 为挂起的Model变化添加 ...
- Python之路【第十八篇】Django小项目简单BBS论坛部分内容知识点
开发一个简单的BBS论坛 项目需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可被 ...
- 《大型网站系统与Java中间件实践》读书笔记——CAP理论
分布式事务希望在多机环境下可以像单机系统那样做到强一致,这需要付出比较大的代价.而在有些场景下,接收状态并不用时刻保持一致,只要最终一致就行. CAP理论是Eric Brewer在2000年7月份的P ...
- Activity初接触
Activity中TextView的文字显示Hello Android: 1.直接显示:<TextView android:text="Hello Android" /> ...
- MySQL Cluster 数据分布(分区、分组)
数据分布 1.MySQL Cluster自动分区数据表(也可能使用用户自定义分区),将数据分布到分区中: 2.一个数据表被划分到多个Data Node分区中,数据在分区中被”striped”: 3.主 ...
- r-cnn学习(六):RPN及AnchorTargetLayer学习
RPN网络是faster与fast的主要区别,输入特征图,输出region proposals以及相应的分数. # ------------------------------------------ ...