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 ...
随机推荐
- Computer Science Courses – Yan Yan
CS: Compilers / Programming Languages Course Title Fundamentals of C++ Language Programming Textbook ...
- JDE报表开发笔记(数据选择及继承)
在Section的Event中, Do Custom Section("sectionxxx")自定义加载下一个Section Set Selection Append Flag( ...
- FZU 1914 Funny Positive Sequence
题目链接:Funny Positive Sequence 题意:给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个数列的这n种变换里, A(0): a1, ...
- 学习shell之前你不得不了解的小知识
1.!!命令 # 表示上一条输出history |grep 23 #表示历史记录中的第23条!vim #上一 ...
- javaMail创建邮件和发送邮件总结
(注: 本文是参考http://www.cnblogs.com/xdp-gacl/p/4216311.html. 感谢博主的精彩的描述) 一, 前期的准备 1, 导入 mail.jar 二, 操作步骤 ...
- 转: JSP中include指令和include动作的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改,否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如果 ...
- 一模 (4) day2
第一题: 题目大意:二进制数 n mod m 的结果是多少? n 的长度(二进制数的位数)<=200 000: m 的长度(二进制数的位数)<=20. 解题过程: 1.我的算法是直接高 ...
- myeclipse中导入的jquery文件报错(出现红叉叉,提示语法错误)
转自:http://blog.csdn.net/simplty/article/details/7661504
- redis Ok2
Redis::__construct 描述: 创建一个Redis客户端 范例: $redis = new Redis(); connect, open 描述: 实例连接到一个Redis. 参数:h ...
- 【第41套测试题NOIP2007】【排序】【DP】【高精度】【树】【图上路径】
先说点题外话,这两天的入学考试,炸了……语文有史以来最差,数学有史以来最差……还有4科,估计全炸……悲痛的心情,来调程序.这套题是8.31考的,从昨天晚上开始改的,因为第三题迟迟不想写,才拖到了现在. ...