作者: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] ...
随机推荐
-
Android Screen Monitor抓取真机屏幕
今天看到一款有点意思的开源软件“android-screen-monitor”, 简要记录如下: 1 简介 一款同步手机真机屏幕到PC上的软件(屏幕实时抓取,有点小卡) 2 开源地址 http://c ...
-
core&mdash;线程与IO
CPU执行线程期间,从内存里调用指令,然后运行,这些指令有可能要从硬盘里面,网络里,读取数据.我们知道在计算机硬件体系中,从内存读取数据的速度会大于从硬盘或网络里面的速度.线程必须要等到硬盘里面的数据 ...
-
剑指offer
今天完成了剑指offer上的66道编程题,感觉自己还是很多代码实现能力和算法积累都还不够!还需要继续联系,坚持自己独立写代码实现. 最后将今天的两道题目奉上,都有异曲同工之妙: 矩阵中的路径: #in ...
-
【Zookeeper】源码分析之服务器(四)
一.前言 前面分析了LeaderZooKeeperServer,接着分析FollowerZooKeeperServer. 二.FollowerZooKeeperServer源码分析 2.1 类的继承关 ...
-
angularjs ng-class
ng-class指令可以设置一个键值对,用于决定是否添加一个特定的类名,键为class名,值为bool类型表示是否添加该类名 <style> .red { color: red; } .g ...
-
链路层 - SLIP,PPP,
最常使用的封装格式是RFC 894定义的格式.图2 - 1显示了两种不同形式的封装格式.图中每个方框下面的数字是它们的字节长度. 两种帧格式都采用48 bit(6字节)的目的地址和源地址( 8 0 2 ...
-
struts2 拦截器弊端
struts2 怎样在action内获得fielderror
-
【咸鱼教程】TextureMerger1.6.6 一:Egret MovieClip的制作和使用
几个月没写代码了.然后突然用到TextureMerger,发现不会用序列图做动画了... 于是写下过程,以防忘记... MovieClip主要是用于游戏中的动画制作和播放.支持swf.gif.序列图等 ...
-
CyanogenMod---android
http://blog.csdn.net/sheldon4090/article/details/7736957--service. android进程间通信:使用AIDL http://blog.c ...
-
C#调用 Oracle 存储过程样例代码
-- 建表 CREATE TABLE sale_report ( sale_date DATE NOT NULL , sale_item VARCHAR(2) NOT NULL , ...