leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html
题目链接:leetcode Permutations II 无重全排列
题目要求对有重复的数组进行无重全排列。为了保证不重复,类似于全排列算法使用dfs,将第一个数字与后面第一次出现的数字交换即可。例如对于序列1,1,2,2
有如下过程:
,1,2,2 -> 1,,2,2 -> 1,1,,2 -> 1,1,2,
-> 1,2,,2 -> 1,2,1,
-> 1,2,2,
-> 2,,1,2 -> 2,1,,2 -> 2,1,1,
-> 2,1,2,
-> 2,2,,1 -> 2,2,1,
代码中使用set来记录数字是否在前面出现过,如果出现过,则不与第一个数字进行交换。
代码如下:
class Solution {
public:
void swap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
void p(vector<int> &num, vector<vector<int> > &res, int begin)
{
if( begin == num.size() )
{
vector<int> tmp;
for( int i = ; i < num.size() ; i++ )
{
tmp.push_back(num[i]);
}
res.push_back(tmp);
}
else
{
set<int> used;
for( int i = begin ; i < num.size() ; i++ )
{
if(!used.count(num[i]))
{
used.insert(num[i]);
swap(num[i], num[begin]);
p(num, res, begin+);
swap(num[i], num[begin]);
}
}
}
}
vector<vector<int> > permuteUnique(vector<int> &num)
{
vector<vector<int> >res;
if( num.size() == )
{
return res;
}
sort(num.begin(), num.end());
p(num, res, );
return res;
}
};
leetcode Permutations II 无重全排列的更多相关文章
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] permutations ii 全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode Permutations II (全排列)
题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- leetcode -- Permutations II TODO
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
随机推荐
- java.io.File中的pathSeparator与separator的区别
先总的说一下区别:File.pathSeparator指的是分隔连续多个路径字符串的分隔符,例如:java -cp test.jar;abc.jar HelloWorld就是指“;” Fi ...
- 360开源的类Redis存储系统:Pika
Pika 是 360 DBA 和基础架构组联合开发的类 Redis 存储系统,完全支持 Redis 协议,用户不需要修改任何代码,就可以将服务迁移至 Pika.有维护 Redis 经验的 DBA 维护 ...
- 获取mp4文件信息
计算电影长度 方法1 从mvhd - movie header atom中找到time scale和duration,duration除以time scale即是整部电影的长度. time scale ...
- css动画——transition和animation
http://www.ruanyifeng.com/blog/2014/02/css_transition_and_animation.html 第一部分:CSS Transition 在CSS 3引 ...
- [数据库]Oracle和mysql中的分页总结
Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...
- [gulp] gulp lint 忽略文件
how does the ignore parameter works in gulp and nodemon? 参考了 前端构建工具gulp入门教程,里面的lint我不需要检查所有js下面的文件,因 ...
- 2014年下半年计划—写博客,旅游,带女朋友拍写真
前言:写这篇博客之前,一直在网上,看各位大牛写的博文,发布的视频等.当然由于自己的初来乍到,人生地不"熟"儿的,也吃了不少亏,走了不少弯路.本着一颗学习的心,携着向各 ...
- hive与hbase集成
http://blog.csdn.net/vah101/article/details/22597341 这篇文章最初是基于介绍HIVE-705.这个功能允许Hive QL命令访问HBase表,进行读 ...
- ajax调用服务的基本格式
<个人积累,转载请注明出处> 格式如下: $.ajax({ type: "post", //访问WebService使用Post方式请求 url: "http ...
- ServerRelativeUrl与Url
文件的file.ServerRelativeUrl,是文件相对于网站集的地址,file.Url是文件相对于本网站的地址.