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 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
随机推荐
- linux优化之优化开机自启动服务
过滤出来需要的开机自启动项:chkconfig --list|grep 3:on|grep -v "crond|sshd|network|rsyslog|sysstat" ...
- Lecture 3
surface models 1. The two main methods of creating surface models are interpolation and triangulatio ...
- Python头脑风暴1
发个致富脑洞:我就在想本人虽然单身,但本人恋爱经历很多,追女生技术十足,女朋友漂亮又贤惠.如果本人开个平台帮人诚心介绍女朋友,男女成男女朋友经男方同意我收2.5万(IT界平均月收入的1.5倍不到),双 ...
- LeetCode(129) Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- stm32L0系列学习(二)HAL-LL库等比较
- excel日期格式取年份
具体思路:先将日期格式更改为常规格式,再取常规格式的前4位数字 例如:A1==1981/12/22 第一步B1=TEXT(A1,"emd") 第二步C1=LEFT(B1,4) 结束
- Linux: 正则表达式
正则表达式:正规的表示法,常规的表示法(Regular Expression)正则表达式使用单个字符串来描述,匹配一系列的符合某个句发规则的字符串. 1)命令格式; grep [正则] 字符串 文件 ...
- 【转】基于 Apache 在本地配置多个虚拟主机
如何使用 Apache 在本地配置出多个虚拟主机呢?而且使用不同的“域名”来访问本地不同的站点呢? 一般情况下,咱们都使用 localhost 来访问本机上的服务器,在我们的 C:/WINDOWS/s ...
- bat 中的特殊符号输出问题
系统关键字(感叹号!)冲突 由于是自动化部署,因此需要使用到循环,这里就不可避免的用到了延迟变量(setlocal enabledelayedexpansion) 有关延迟变量的知识,大家可以通过这篇 ...
- 聊聊、Nginx GDB与MAIN参数
接着上一篇,我们学习 Nginx 的 main 方法.用 gdb 工具调试 Nginx,首先 gdb nginx.如下: gdb 调试工具有很多的命令,上一篇为了找 main 方法用了 b 命令,也就 ...