给的一个数列中,可能存在重复的数,比如 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. 洛谷 P3328 【[SDOI2015]音质检测】

    这题我做的好麻烦啊... 一开始想分块来着,后来发现可以直接线段树 首先考虑一个性质,我们如果有数列的相邻两项f[i]和 f[i+1]那么用这两项向后推k项其线性表示系数一定(表示为f[i+k]=a∗ ...

  2. 前端MVVM模式及其在Vue和React中的体现

    MVVM相关概念 Mvvm 前端数据流框架精讲 1) MVVM典型特点是有四个概念:Model.View.ViewModel.绑定器.MVVM可以是单向绑定也可以是双向绑定甚至是不绑定 2) 绑定器: ...

  3. ACM 贪心算法总结

    贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (po ...

  4. source insight

    关于source inlight的版本 http://www.camnpr.com/archives/559.html   最新版本 http://www.sourceinsight.com/upda ...

  5. HDU 4729 An Easy Problem for Elfness 主席树

    题意: 给出一棵树,每条边有一个容量. 有若干次询问:\(S \, T \, K \, A \, B\),求路径\(S \to T\)的最大流量. 有两种方法可以增大流量: 花费\(A\)可以新修一条 ...

  6. 各浏览器对 window.open() 的支持

    原文地址

  7. 模拟 - BZOJ 1510 [POI2006] Kra-The Disks

    BZOJ 1510 [POI2006] Kra-The Disks 描述 Johnny 在生日时收到了一件特殊的礼物,这件礼物由一个奇形怪状的管子和一些盘子组成. 这个管子是由许多不同直径的圆筒(直径 ...

  8. html 标签附加文本属性

    <!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...

  9. Zookeeper在windows环境下安装

    1.已安装JDK并配置好了环境变量 2.下载Zookeeper,在清华大学镜像下载,选择合适版本  https://mirrors.tuna.tsinghua.edu.cn/apache/zookee ...

  10. Java分页内容实例详解

    首先定义一个fruit表,表里含有很多数据: 定义一个数据文件: public class Fruit { public String getIds() { return ids; } public ...