[LeetCode] Permutations II 排列
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].
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
using namespace std; class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
int n = num.size();
vector<vector<int> >ret;
if(n<) return ret;
if(n<) {ret.push_back(num); return ret; }
sort(num.begin(),num.end());
ret.push_back(num);
while(next_permutation(num.begin(),num.end())){
ret.push_back(num);
}
return ret;
}
}; int main()
{
vector<int> num = {,,};
Solution sol;
vector<vector<int> > ret = sol.permuteUnique(num);
for(int i=;i<ret.size();i++){
copy(ret[i].begin(),ret[i].end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
return ;
}
如果不调用嘛,就是自己写一个next_permutation,在Permutations 写过好多个版本了,回顾下stl 的实现逻辑吧:
- 输入数组a[],从右向左遍历,寻找相邻的两个数,使得 left<mid,没找到?就是没有,返回false了。
- 再次从右往左遍历,寻找right >left, 这次的right 不需要一定与left 相连,因为有1 这部,所以有保底的取值(mid)
- 交换left 与right。
- 逆向mid 与其右边。
- 结束。
[LeetCode] Permutations II 排列的更多相关文章
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetcode -- Permutations II TODO
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode:Permutations(求全排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- [Leetcode] 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 permutations ...
随机推荐
- 数据结构-二分查找(Binary Search)
#include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...
- javaWeb开发中常见的问题
1.修改表单提交的时候不好使可能是因为没写对应隐藏域的ID 2.el表达式在js代码中要加“”,例如 "${}" 3.JavaScript中的函数也有重载的特性.如果两个input ...
- 893E - Counting Arrays
E. Counting Arrays time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 2018 Multi-University Training Contest 1 H - RMQ Similar Sequence(HDU - 6305 笛卡尔树)
题意: 对于一个序列a,构造一个序列b,使得两个序列,对于任意的区间 [l, r] 的区间最靠近左端点的那个最大值的位置,并且序列 b 满足 0 < bi < 1. 给定一个序列 a ,求 ...
- 海量数据处理算法—BitMap
1. Bit Map算法简介 来自于<编程珠玑>.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空 ...
- python-PIL模块的使用
PIL基本功能介绍 from PIL import Image from PIL import ImageEnhance img = Image.open(r'E:\img\f1.png') img. ...
- 洛谷P1067 多项式输出
题目链接:https://www.luogu.org/problemnew/show/P1067 这是一个纯模拟的小怪但是需要注意一些小细节: 1.首项为正没有+号. 2.所有项系数如果是一的话就省略 ...
- 单例模式【python】
在python中,如需让一个类只能创建一个实例对象,怎么能才能做到呢? 思路:1.通过同一个类创建的不同对象,都让他们指向同一个方向. 2.让个类只能创建唯一的实例对象. 方法:用到 _ _new ...
- go经典练习题涉及流程控制-字符串-struct-map的数据类型的处理
one:求1到100之间的质数 package main import ( "fmt" ) func isPrime(n int) bool { var flag = true f ...
- 启用hyper后无法打开vmware
十万火急,想办法先让虚拟机能够打开,毕竟经常用. 网上看了无数教程都是让在控制面板中关闭hyper-v,然而并没有用. 找了好久说是不能那样关闭,得用指令.管理员运行powershell,输入下列指令 ...