[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 ...
随机推荐
- goaccess分析access.log
接上一篇,开始学习goaccess使用~ 源码安装完成后,我的goaccess的配置文件goaccess.conf位于/usr/local/etc/ /usr/local/etc/goaccess/g ...
- IE支持直接查看Json数据注册表代码
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json] ...
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器
<?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ p ...
- Python文件与异常处理
文件读写 使用python的BIF(build in function)open()进行文件读写操作 # 1.打开文件 data = open(file_name,'w') # 读取模式有很多种,主要 ...
- Android开发——常见的内存泄漏以及解决方案(二)
)Android2.3以后,SoftReference不再可靠.垃圾回收期更容易回收它,不再是内存不足时才回收软引用.那么缓存机制便失去了意义.Google官方建议使用LruCache作为缓存的集合类 ...
- IOS开发学习笔记037-九宫格代码实现
九宫格布局,用手机输入法时经常见到.先按3行3列写. 代码的实现主要是计算插入图片的位置. 每一张图片的位置和所在的行列密切相关.分析过程如下: 界面: 代码实现 1.把需要的图片资源添加进来 然后给 ...
- ogre3D学习基础2 -- 顶点程序与片断程序
三.顶点程序与片断程序 顶点或者片断程序定义可以被多个材质使用,唯一的前提条件是必须在引用它之前在材质的渲染通路部分中定义. 一个低级顶点程序示例如下: vertex_program myVertex ...
- C++ map 的用法归纳2
[尊重原著: http://blog.csdn.net/zcf1002797280/article/details/7847819] Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序 ...
- 快速获取Android应用包名和Activity名
一.获取包名 方法1: 先说明一下这里讲的方法是通用的,而网上其他方法获取PackageName不通用(因为他是建立在root的基础上的,我不敢保证你的设备已经root). ①在android设备上点 ...
- Python学习-day16-DOM
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...