[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 ...
随机推荐
- ED2k Resource
http://www.lwkk.com/ http://www.ed2000.com/
- linux下重启apache
基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/local/apache2/bin/apachec ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 【Beta】七天屠蛟记
团队名字: 一不小心就火了 屠龙天团少年们: 031402504 陈逸超 (组长) 031402505 陈少铭 031402511 黄家俊 031402515 翁祖航 031402516 黄瑞钰 03 ...
- Charles抓Android的数据包
1. 获得Mac OS的IP地址 2. 对Android手机设置代理,主机IP是步骤1中获得的IP,端口8888
- Windows XP系统下添加任务计划常出现问题解决办法
Windows XP系统下添加任务计划常出现问题解决办法 计划任务就是让电脑在指定的时间内执行指定的动作(计划动作),这些动作可以是一个程序,也可以是一个批处理,但是至少是可以运行的(通俗一些就是双击 ...
- ActiveMQ启动多个broker
具体步骤如下: 1.把activemq目录下的conf文件复制一份,叫做conf2, 命令: cp -r conf conf2 2.修改conf2目录下的activemq.xml文件 a.修改brok ...
- C# 读取excel日期时获取到数字转换成日期
string strDate= DateTime.FromOADate(Convert.ToInt32(data[i][7])).ToString("d"); strDate= D ...
- 大熊君学习html5系列之------History API(SPA单页应用的必备------重构完结版)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- tyvj1194 划分大理石
描述 有价值分别为1..6的大理石各a[1..6]块,现要将它们分成两部分,使得两部分价值之和相等,问是否可以实现.其中大理石的总数不超过20000. 输入格式 有多组数据!所以可能有多行如果有0 ...