LeetCode OJ--Permutations II
给的一个数列中,可能存在重复的数,比如 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的更多相关文章
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- Java for LeetCode 047 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 047 Permutations II
题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 47 Permutations II(全排列)
题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
随机推荐
- 【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path
有点像计蒜之道里的 京东的物流路径 题目描述 给定一棵 N 个节点的树,每个节点有一个正整数权值.记节点 i 的权值为 Ai.考虑节点 u 和 v 之间的一条简单路径,记 dist(u, v) 为其长 ...
- MySQL数据库---索引
索引的作用就是快速找出在一个列上用一特定值的行.如果没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行. 索引的类型: 先写一个建表语句: CREATE TABLE `t ...
- 【Python学习之七】递归——汉诺塔问题的算法理解
汉诺塔问题 汉诺塔的移动可以用递归函数非常简单地实现.请编写move(n, a, b, c)函数,它接收参数n,表示3个柱子A.B.C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的 ...
- 02等待单个线程返回WaitForSingleObject
windows 多线程之等待线程返回 多线程编程中,有时我们需要等待某一线程完成了特定的操作之后再继续做其他事情,要实现这个目的,可以使用 Windows API 函数 WaitForSingle ...
- CentOS 系统下Gitlab搭建与基本配置 以及代码备份迁移过程
GitLab 是一个开源的版本管理系统,提供了类似于 GitHub 的源代码浏览,管理缺陷和注释等功能,你可以将代码免费托管到 GitLab.com,而且不限项目数量和成员数.最吸引人的一点是,可以在 ...
- destoon修改手机端分页
1. global.func.php pages函数和listpages函数 函数开头增加 $DT_TOUCH,$newsamplepages变量 global $DT_URL, $DT, $L,$D ...
- R-data.table
data.table可以扩展和增强data.frame的功能,在分组操作和组合时访问速度更快. require(data.table) theDT = data.table(A=1:10, B=let ...
- XML映射文件中关系映射
映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工表: DROP ...
- dedecms 搬家流程
进入后台 系统 点击数据备份/还原根据新空间数据库版本格式备份 进入网站根目录备份文件夹data\backupdataimagestempletsuploadsplus 进入新空间 重新安装dede程 ...
- 加密javascript代码
最近看了个js日历,里面用到了加密,看了下,自己也模仿做加密,现在只能加密一般的javascript语句 <!DOCTYPE html> <html> <meta htt ...