给的一个数列中,可能存在重复的数,比如 1 1  2 ,求其全排列。

记录上一个得出来的排列,看这个排列和上一个是否相同。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution{
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > ans;
if(num.size()==)
return ans; vector<int> _num = num;
sort(_num.begin(),_num.end()); vector<int> _beforeOne = _num;
ans.push_back(_num);
while(nextPermutation(_num))
{
if(_num != _beforeOne)
{
ans.push_back(_num);
_beforeOne = _num;
}
}
return ans;
}
private:
bool nextPermutation(vector<int> &num)
{
return next_permutation(num.begin(),num.end());
} template<typename BidiIt>
bool next_permutation(BidiIt first,BidiIt last)
{
const auto rfirst = reverse_iterator<BidiIt>(last);
const auto rlast = reverse_iterator<BidiIt>(first); auto pivot = next(rfirst);
while(pivot != rlast && *pivot >= *prev(pivot))
{
++pivot;
} //this is the last permute, or the next is the same as the begin one
if(pivot == rlast)
{
reverse(rfirst,rlast);
return false;
}
//find the first num great than pivot
auto change = rfirst;
while(*change<=*pivot)
++change; swap(*change,*pivot);
reverse(rfirst,pivot);
return true;
}
}; int main()
{
vector<int> num;
num.push_back();
num.push_back();
num.push_back(); Solution myS;
myS.permute(num);
return ;
}

LeetCode OJ--Permutations II的更多相关文章

  1. 【leetcode】Permutations II

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

  2. Java for LeetCode 047 Permutations II

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

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. leetCode 47.Permutations II (排列组合II) 解题思路和方法

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

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

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

  6. LeetCode 047 Permutations II

    题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...

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

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

  8. [leetcode] 47. Permutations II

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

  9. 【leetcode】Permutations II (middle)

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

  10. LeetCode 47 Permutations II(全排列)

    题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description   给出数组,数组中的元素可能有重复,求出所有的全排列   使 ...

随机推荐

  1. destoon手机端mobileurl函数增加城市分类参数

    mobileurl函数在include/global.func.php 858行 共四个参数,moduleid-模型id,catid-分类id,itemid -文章id,page-页码 functio ...

  2. HTML5一些特殊效果分享地址集合

    页面预加载图片原生js: http://www.cnblogs.com/st-leslie/articles/5274568.html HTML5 FileReader读取本地文件: http://n ...

  3. json_encode() 避免转换中文

    json_encode() 避免转换中文 我们都知道,json_encode()可以将数据转换为json格式,而且只针对utf8编码的数据有效,而且在转换中文的时候,将中文转换成不可读的”\u***” ...

  4. 将php数组转js数组,js如何接收PHP数组,json的用法

    首先下载下面这个文件(这是一段是别人写出来专门解析json的代码),然后引入这个文件! http://pan.baidu.com/s/1dD8qVr7 现在当我们需要用ajax与后台进行交互时,怎样将 ...

  5. Python头脑风暴2

    今天想到了一个致富新途径:假如我在X东上班,我写个X宝爬虫,专门爬在X宝买奢侈品的土豪,然后我自己注册个X宝号,用脚本一个个加他们然后给他们发信息说我X东这还有比你更便宜更好的...不知道行不行啊(狗 ...

  6. 汇编语言 Part 1——简介、基本语法、内存分段与内存地址

    简介 什么是汇编语言? 汇编语言是一种低级的编程语言,在程序的语句和体系结构的机器代码指令之间有很强的对应关系. 每种汇编语言都特定于特定的计算机体系结构,但需要解释或编译.汇编语言也可以称为符号机器 ...

  7. Tomcat Bug记录

    1.问题:org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-IN ...

  8. JAVA基础篇—String和StringBuffer

    区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringBuffer类对象为可修改对象,可以通过append() ...

  9. atag信息处理

    machine_desc->boot_params参数保存的是u-boot传入的启动参数的地址,如果没有传入启动参数,使用如下的默认参数: /* * This holds our default ...

  10. printk的使用技巧

    在 linux/kernel.h 中有相应的宏对应. #define KERN_EMERG    "<0>"    /* system is unusable */#d ...