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. 引领技术变革,腾讯云、腾讯WeTest和英特尔,合作布局云游戏

    WeTest 导读 ChinaJoy作为中国泛娱乐产业年度风向标,受到全球业界的高度关注.在本届ChinaJoy上,腾讯云.腾讯WeTest和英特尔,合作为游戏玩家.游戏开发者等业界人士联合展出了云游 ...

  2. C# 组装XML传给webserver+XML 返回获取多个xml,根据多个XML 返回dataset类型

    大致流程介绍: 传值给 webserver+XML ,得到webserver+XML多个返回值,组装成dataset形式返回 首先创建所需要的类型 DataSet ds = new DataSet() ...

  3. Linux用户切换和密码修改

    1.普通用户切换到root su - 再输入root密码,密码正确,成功切换,再输入exit则切换回普通用户 2.root切换到其他用户,例user su - user 再输入exit,则切换回roo ...

  4. Qt-QML-关于两个平级的qml文件中的函数调用问题

    这几天还在继续搞我的QML,感悟就QML是坑的同时,也是一门很号的语言,用于快速搭界面是很好的.那么,这几天, 遇到一个问题,在下用一个框框画一下,希望可以理解 抽象派,解释一下,QML1和QML3是 ...

  5. Java开发工程师(Web方向) - 04.Spring框架 - 第5章.Web框架

    第5章--Web框架 Web框架概述 Web框架单元测验 本次得分为:13.50/15.00, 本次测试的提交时间为:2017-09-25 1单选(2分) 关于Spring MVC中Dispatche ...

  6. leetcode-汉明距离

    汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  7. yarn logs -applicationId命令java版本简单实现

    import java.io.DataInputStream; import java.io.EOFException; import java.io.FileNotFoundException; i ...

  8. CsvHelper文档-2读

    CsvHelper文档-2读 这个库默认不需要做任何设置就可以很容易的使用它.如果你的类属性名称直接匹配csv的标题名称,那么可以按照下面的实例来用: (以下所有的代码都需要引用using csvhe ...

  9. 性能度量之Confusion Matrix

    例子:一个Binary Classifier 假设我们要预测图片中的数字是否为数字5.如下面代码. X_train为训练集,每一个instance为一张28*28像素的图片,共784个features ...

  10. solidity python 签名和验证

    注意,以太坊智能合约里面采用的是公钥非紧凑类型 def gen_secrets_pair(): """ 得到公钥和私钥 :return: ""&quo ...