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 ...
随机推荐
- 生成json对象
JSONObject 对于放入的object,最终生成的json是什么样的? 两个JavaBean: public class ClassBean { private int grade; priva ...
- Android 适配器
Adapter是连接后端数据和前端显示的桥梁,是数据和UI(View)之间的纽带. 在常见的View(ListView,GridView)等地方都需要用到Adapter.数据.Adapter ...
- 简单分析Java的HashMap.entrySet()的实现
关于Java的HashMap.entrySet(),文档是这样描述的:这个方法返回一个Set,这个Set是HashMap的视图,对Map的操作会在Set上反映出来,反过来也是.原文是 Returns ...
- 【转】HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- PowerMock使用遇到的问题——1
遇到问题:再用PowerMock Mock构造方法时,所有语句都可以通过执行,但当最后执行verify语句时却总是出现如下错误: java.lang.AssertionError: ...
- C# DES加密
需要引用名称空间 using System; using System.Text; using System.Security.Cryptography; using System.IO; 具体代码: ...
- ARC 类型转换:显式转换 id 和 void *
http://blog.csdn.net/chinahaerbin/article/details/9471419 /* * ARC有效时三种类型转换: */ 1.__bridge ...
- javaweb-四则运算
这次作业,我们选择的是网页开发,后来我们小组才知道自己这方面的知识还是太匮乏了. 主要代码: public class calcu extends HttpServlet{ public void d ...
- linux下格式化硬盘与挂载硬盘
格式化: mkfs -t ext4 /dev/sdb 自动挂载: 编辑/etc/fstab文件 sudo nano /etc/fstab,如下图将设备/dev/sdb硬盘挂载到/home/solr/s ...
- Cocoapods的安装与使用
一.安装 1.CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境.OS X系统默认已经可以运行Ruby了,因此我们只需执行以下命令: sudo gem install cocoa ...