LeetCode Permutations II (全排列)
题意:
给出n个元素(可能有重复的),请产生出所有的全排列。
思路:
同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点。
如果将一个序列中两个相同的元素交换,这个序列是仍然没有发生改变的,这也是省时间的关键点。考虑第i个位置可取的元素是nums[i-1,nums.size()-1],那么交换的时候不必要将与num[i-1]相同的元素交换到第i位了。这可以预先通过排一次序解决。排序后变成多段相同的元素接在一起,而通常只会将每段的第一个元素被换到第i个位置,考虑每段的第2个时就会检测到相同的,自然不会交换了。
先观察一下代码的执行过程,会很好理解的。
递归:
class Solution {
vector<vector<int> > ans;
public:
void DFS(vector<int> num,int pos)//注意第一个参数不能为引用
{
if(pos+==num.size()) ans.push_back(num);
else
{
for(int i=pos; i<num.size(); i++)
{
if(i!=pos && num[i]==num[pos]) continue;//注意这里
swap(num[i],num[pos]);
DFS(num,pos+);
}
}
}
vector<vector<int> > permuteUnique(vector<int> &nums)
{
sort(nums.begin(),nums.end());
if(!nums.empty()) DFS(nums,);
return ans;
}
};
AC代码
迭代:模拟了STL中的nextPermutation函数。速度很慢。
class Solution {
public:
bool nextPermute(vector<int> &nums){
int i=nums.size()-;
while(i> && nums[i-]>=nums[i]) i-- ;//检查是否已经降序了。
if (i==) return false;
i--;
//需要在i后面找第一个比它大的来跟nums[i]交换,
int j=nums.size()-;
while(j>i && nums[j]<=nums[i]) j--;
swap(nums[i], nums[j]);//交换他们,并使这一段都是升序。
sort(nums.begin()+i+, nums.end());
return true;
}
vector<vector<int> > permuteUnique(vector<int> &nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
res.push_back(nums);
while (true)
{
if(!nextPermute(nums)) break;
res.push_back(nums);
}
return res;
}
};
AC代码
LeetCode Permutations II (全排列)的更多相关文章
- [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 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [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 ...
随机推荐
- 分形树Fractal tree介绍——具体如何结合TokuDB还没有太懂,先记住其和LSM都是一样的适合写密集
在目前的Mysql数据库中,使用最广泛的是innodb存储引擎.innodb确实是个很不错的存储引擎,就连高性能Mysql里都说了,如果不是有什么很特别的要求,innodb就是最好的选择.当然,这偏文 ...
- ExecuteNonQuery()返回值注意点
在使用ExecuteNonQuery(),调用存储过程,语句执行无错误,但是返回结果一直是-1 原因: 当使用储存过程时, 要把SET NOCOUNT ON 这个语句去掉, 这样数据就有反回值了 当 ...
- 161018--NOIP模拟
老实说,感觉自己好菜啊..(安慰自己省选做多了 T1:看似1e6很大,实际上常数52都能草过去...不知为何RE.. T2:记忆化搜索.看错题目条件QAQ,其实把自己暴力搜的程序改改就好了.. T3: ...
- sql删除多余重复的数据只保留一条
delete from people where peopleName in (select peopleName from people group by peopleName ...
- js计算日期的前几天的日期
月份0---11 var date = new Date(year,fenye_arr[0]-1,fenye_arr[1]); miao=date.getTime(); var ...
- S1 :闭包
闭包是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见方式,就是在一个函数内部创建另一个函数,以createComparisonFunction()函数为例 function createCo ...
- 类成员函数作为pthread_create函数参数
from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...
- 一模 (1) day2
第一题:(水题) 题目大意:就是给出扫雷的图,然后统计每个九宫格的雷的个数. 解题过程: 1.好久没做这样的水题了.直接模拟水过.. 第二题: 题目大意:给出一个长度小于1000的数k,要求一个尽可能 ...
- 【第53套模拟题】【递推】【RMQ】【二进制】【分块】
题目:(开始自己描述题目了...) 第一题大意: 求1~n的所有排列中逆序对为k个的方案数,输出方案数%10000,n<=1000. 解:这道题一个递推,因为我基本上没怎么自己做过递推,所以推了 ...
- 常见的Web负载均衡方法
用户手动选择 通过在主站首页入口提供不同线路,不同服务器链接的方式,来实现负载均衡.在一些提供下载业务的网站中比较常见,如:华军软件园. DNS轮询 大多域名注册商都支持对同一主机名添加多条A记录,这 ...