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] ...
随机推荐
- CAS Proxy 的相关文章
cas代理模式的原理及配置 http://my.oschina.net/mashiguang/blog/69312 剖析CAS Proxy的设计原理 http://www.blogjava.net/s ...
- ios 说一说UINavigationController 的堆栈
iOS的界面堆栈管理比android的要好用很多. 这里写两点:一点是 如何重回前面的vc,而不是push一个alloc的新界面.第二点就是判断当前堆栈显示的vc是何vc. vc堆栈: vc1-> ...
- node.js在windows下的学习笔记(6)---安装Express
Express是什么呢? express.js是nodejs的一个MVC开发框架,并且支持jade等多种模板.对于WEB应用程序而言,会有许多诸如模板和路由这样的公共模式在的,虽然也可以自己编写代码解 ...
- Android中由IP地址查询经纬度坐标的实例
大家都知道,根据IP地址就可以知道它所在的具体位置,在Android中同样可以由IP地址得到它的位置,即具体的地理经纬度坐标. 本文就直接以代码的方式演示如何根据IP地址查询地理经纬度坐标位置,下面的 ...
- redis实现spring-redis-data的入门实例
redis的客户端实现.主要分为spring-redis-data .jredis. 记录下spring-redis-data的学习心得:spring-redis-data 中我目前主要用了它的存.取 ...
- java中的url 编码与解码
什么是application/x-www-form-urlencoded字符串? 答:它是一种编码类型.当URL地址里包含非西欧字符的字符串时,系统会将这些字符转换成application/x-www ...
- HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决
又是折腾了一天才解决的问题,网上关于这个问题的资料不多,希望写出来能帮到有需要的人. 之前无论怎么设置charset都不起作用, 后来看了这篇文章 才发现MultipartEntityBuilder有 ...
- 关于Android模拟器键盘不能使用的解决方法
很多朋友遇到一个问题,自己搭建完了Android环境后,启动模拟器体验Android系统,但是发现不能使用键盘方便的输入内容,如下图: 同时,使用笔记本的键盘也无法输入内容,只能通过模拟器内置的输入法 ...
- C#数据采集类
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- C# 工厂模式示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 工厂模式 ...