【LeetCode】47. Permutations II
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]
, and [2,1,1]
.
首先分析一下与Permutations有何差异。
记当前位置为start,当前排列数组为cur
1、cur[start]与cur[start]值相同的元素交换位置会产生大量重复。
如:1,3,2,1
两个1互换位置之后,后续的所有排列都是重复的。
2、cur[start]与其他相同值的元素多次交换位置会产生大量重复。
如:1,2,3,2
1与两个2互换位置后,后续的所有排列都是重复的。
因此改变在于:
对于同一个值,只交换一次,否则跳过。
为了保证这一点,必须对cur数组start位置之后的元素排序,这样可以跳过重复元素。
若不排序会产生如下问题:
0,0,1,9 --> 9,0,1,0
0就不连续了,无法进行判断去重,结果又会产生重复。
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > ret;
Helper(ret, num, );
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> num, int pos)
{
if(pos == num.size()-)
ret.push_back(num);
else
{
sort(num.begin()+pos, num.end());
for(int i = pos; i < num.size(); i ++)
{
if(i != pos && num[i] == num[i-])
continue;
swap(num[pos], num[i]);
Helper(ret, num, pos+);
swap(num[pos], num[i]);
}
}
}
};
【LeetCode】47. Permutations II的更多相关文章
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- 【LeetCode】047. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】基本计算器II
[问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 .整数除法仅保留整数部分. 输入: "3+2*2" ...
- 【LeetCode 】N皇后II
[问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...
- 【LeetCode】跳跃游戏II
[问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...
随机推荐
- Redis五大数据结构
1.Redis介绍 Redis是REmote DIctionary Server的缩写,作者定位于一个内存KV存储数据库(In-memory key-value Store),让Redis自豪的并不是 ...
- centos6.8安装具有ngx_cache_purge模块的nginx1.10.3
CentOS-6.8 安装 Nginx1.10.3Nginx 环境准备:安装Nginx需要完成以下依赖的安装 1.gcc 安装:yum install gcc-c++ 2.PCRE pcre-deve ...
- CentOS 加载/挂载 U盘 (转)
原文链接:CentOS 加载/挂载 U盘 Linux如何加载(优)U盘 1,以root用户登陆 先加载USB模块 modprobe usb-storage 用fdisk -l 看看U盘的设 ...
- HTML标签 闭合还是不闭合?
你在写 HTML5 代码的时候,是否纠结过应该写 <br /> 还是 <br>,是写 <input /> 还是写 <input>.写 <scrip ...
- 变量声明置顶规则、函数声明及函数表达式和函数的arguments属性初始化
一.变量声明和变量赋值: if (!("a" in window)) { ; } alert(a);//a为? 你可能认为alert出来的结果是1,然后实际结果是“undefine ...
- operator++()和operator++(int)的区别
很久以前(八十年代),没有办法区分++和--操作符的前缀与后缀调用.这个问题遭到程序员的报怨,于是C++语言得到了扩展,允许重载increment 和 decrement操作符的两种形式. 然而有一个 ...
- SQL Server 视图索引
在视图上创建索引的另一个好处是:查询优化器开始在查询中使用视图索引,而不是直接在 FROM 子句中命令视图.这样一来,可从索引视图检索数据而无需重新编码,由此带来的高效率也使现有查询获益.在视图上创建 ...
- GridControl 分组排序
方法一:纯代码 this.list.gridControl.ItemsSource = lsItem; this.list.gridControl.GroupBy("GroupTitle&q ...
- 转: 网卡名字eth0,eth1的修改方法
转自:http://longwind.blog.51cto.com/419072/982738 我使用这个方法生效: 现象:只有eth2, vi /etc/udev/rules.d/70-per ...
- easyui datagrid种编辑器combobox选择的值不显示解决方案
var combobox_json = [{ "combobox_value" : "GDLB01", "combobox_name" : ...