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 ...
随机推荐
- js在前端获取在本地上传图片的尺寸
var MyTest = document.getElementById("upload_img").files[0];var reader = new FileReader(); ...
- BZOJ1393 [Ceoi2008]knights
题意...上ceoi官网看吧... 首先打一下sg函数发现必胜态和必败态的分布位置是有规律的 于是我们只要知道最长步数的必胜态和最长步数的必败态哪个更长就可以了 然后再打一下步数的表...发现必败态的 ...
- Internet Explorer已限制此网页运行可以访问计算机的脚本或ActiveX控件
在制作网页的时候,大家不免要用到script,也即是脚本,主要是VBScript以及JavaScript.那么时常遇到这样的情况: 在本地双击打开html文件时,如果是IE的话,会出现提示框(如下图) ...
- 0302IT行业就业&软件工程之我所思和所想
阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...
- PHP慢脚本日志和Mysql的慢查询日志(转)
1.PHP慢脚本日志 间歇性的502,是后端 PHP-FPM 不可用造成的,间歇性的502一般认为是由于 PHP-FPM 进程重启造成的. 在 PHP-FPM 的子进程数目超过的配置中的数量时候, ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
- POJ 1436 Horizontally Visible Segments
题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条 与其他线段没交点. 最后问有多少组 3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...
- 第48套题【tarjan】【图&树的连通性】【并查集】
Problem 1 图的连通性
- JDBC Thin Driver 的formats三种格式
格式一: Oracle JDBC Thin using a ServiceName: jdbc:oracle:thin:@//<host>:<port>/<servic ...
- 本节向大家介绍一下UML建模误区
本节向大家介绍一下UML建模误区,这里向大家介绍九个误区,希望通过本节的学习,你对UML建模有清晰的认识,以免在以后使用过程中产生不必要的麻烦.下面让我们一起来看一下这些建模误区吧. UML建模误区 ...