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

For example,
[1,1,2]have the following unique permutations:
[1,1,2],[1,2,1], and[2,1,1].

题意:当数列中有重复数字时,求其全排列。

思路:和permutation一样。以[1,1,2]为例,以第一个1为开始,得到[1,1,2],[1,2,1],。因为排列是将顺序的,所以我们以第二1开始时,若是不去重,则得到的和以第一个1开始时一样,所以,这时,我们要跳过那些和前面相同的数字。这时,我们应该先对数列进行排序,然后,在向中间变量存入数字的时候,若此次遍历中,前面的没有访问过,跳过即可。参看了Grandyang的博客。代码如下:

 class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num)
{
vector<vector<int>> res;
vector<int> tempValue;
vector<int> visited(num.size(),);
sort(num.begin(),num.end()); //对其进行排序
helper(num,,visited, tempValue,res);
return res;
} void helper(vector<int> &num,int level,vector<int> visited, vector<int> &tempValue,
vector<vector<int>> &res)
{
if(level==num.size())
res.push_back(tempValue);
else
{
for(int i=;i<num.size();++i) //这里i=0
{
if(i>&&num[i]==num[i-]&&visited[i-]==) //在下一个if内也行
continue;
if(visited[i]==)
{
visited[i]=;
tempValue.push_back(num[i]);
helper(num,level+,visited,tempValue,res);
tempValue.pop_back();
visited[i]=;
}
}
}
}
};

[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 无重全排列

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

  3. LeetCode: Permutations II 解题报告

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

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

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

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

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

  6. LeetCode Permutations II (全排列)

    题意: 给出n个元素(可能有重复的),请产生出所有的全排列. 思路: 同版本1的有点不同,这次有可能含有重复的元素,很容易就TLE,节省时间才是关键点. 如果将一个序列中两个相同的元素交换,这个序列是 ...

  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. 从golang的垃圾回收说起(上篇)

    本文来自网易云社区 1 垃圾回收中的重要概念 1.1 定义 In computer science, garbage collection (GC) is a form of automatic me ...

  2. NNLearning阶段性总结01

    神经网络最基本的元素与计算流程: 基本的组网原则: 神经网络监督学习的基本步骤: 初始化权值系数 提取一个样本输入NN,比较网络输出与正确输出的误差 调整权值系数,以减少上面误差——调整的方法对应不同 ...

  3. 2019年猪年海报PSD模板-第二部分

    14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1sH0U5oU-LQlaxnaWxPCrrg              

  4. 初识java atomic

    2018-8-19 昨天看到java.util.concurrent.atomic相关的文章,之前有过留意但并未去了解,正好有空学习一下.本人理解atomic包是concurrent子包,当是为并发所 ...

  5. 在Unity中使用LitJson解析json文件

    LitJson 这个库需要找资源,找到LitJson.dll后将它放在Assets文件夹下,在脚本中使用using引入即可 测试代码 json文件: {"Archice":[{&q ...

  6. HDU - 3415(DP + 单调队列)

    链接:HDU - 3415 题意:给出一个包含 n 个数的环,求满足长度大于 0 小于等于 k 的最大区间和. 题解:将数组加倍,形成环.求一个前缀和sum.枚举每一个sum[i],以 i 结尾的最大 ...

  7. 安迪的第一个字典 (Andy's First Dictionary,UVa10815)

    题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...

  8. ubuntu server guide 学习笔记

    1. 软件包 1.1. dpkg dpkg -l dpkg -l | grep apache2 dpkg -L ufw dpkg -S /etc/host.conf dpkg -i zip_3.0-4 ...

  9. Dictionary tabPage使用

    public override bool AccptChange() { //if (oldvalue == null || oldvalue.Count <= 0) //{ // return ...

  10. js随机数算法

    function rnd( seed ){ seed = ( seed * 9301 + 49297 ) % 233280; //为何使用这三个数? return seed / ( 233280.0 ...