题意:

  给出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 (全排列)的更多相关文章

  1. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [Leetcode] permutations ii 全排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  4. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  5. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. [leetcode]Permutations II @ Python

    原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...

  8. leetcode -- Permutations II TODO

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. [Leetcode] Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. ACTIVITI 源码研究之命令模式执行

    ACTIVITI 是一个优秀开源软件,通过阅读源码,我们不但可以了解工作流引擎执行的原理还可以增加个人的编码功力. ACTIVITI 所有执行过程都是采用命令模式进行执行. 本文主要描述流程引擎数据保 ...

  2. 【模板下载】innosetup 制作.net安装包的模板

    NetworkComms网络通信框架序言 这个模板是在博客园和CodeProject上的代码修改而成的,感谢原作者 模板是2个 innosetup 制作.net 2.0 安装包的模板 innosetu ...

  3. SqlServer 慢查询分析优化

    分三步: 记录慢查询的语句到日志文件 1.首先在SSMS,工具菜单下打开Profiler. 2.输入你用户名密码登陆. 3.常规,勾选保存到文件,选择一个文件路径,设置文件大小,这样可以分文件存储日志 ...

  4. qml json 解析到 ListView

    https://github.com/kromain/qml-utils/tree/master/JSONListModel 非常棒!! 实现的原理如下文: http://goessner.net/a ...

  5. qt--- vs

    qt with vs 1.安装vs2012: 2.下载Qt 5.2.0 for Windows 32-bit (VS 2012, 579 MB) 和 Visual Studio Add-in 1.2. ...

  6. Xwindow 连接 RHEL 5

    cd /etc/gdm/custom.conf ----------------------------------------- [security] AllowRemoteRoot=true [x ...

  7. Assert断言测试

    assert编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真.可以在 ...

  8. 开始→运行(cmd)命令大全

    gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 logoff---------注 ...

  9. 1800: [Ahoi2009]fly 飞行棋

    #include<cstdio> #include<iostream> using namespace std; ],n,ans; int main() { scanf(&qu ...

  10. 学习KMP算法的一点小心得

    KMP算法应用于 在一篇有n个字母的文档中 查找某个想要查找的长度为m的单词:暴力枚举:从文档的前m个字母和单词对比,然后是第2到m+1个,然后是第3到m+2个:这样算法复杂度最坏就达到了O(m*n) ...