【Next Permutation】cpp
题目:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
代码:
class Solution {
public:
void nextPermutation(vector<int> &num) {
const int len = num.size();
if (len<) return;
std::vector<int>::iterator pivot=num.end();
// find the pivot pointer
for (std::vector<int>::iterator i = num.end()-; i != num.begin(); --i)
{
if ( *i>*(i-) )
{
pivot = i-;
break;
}
}
// if the largest, directly reverse
if ( pivot==num.end() )
{
std::reverse(num.begin(), num.end());
return;
}
// else exchange the value of pivot and it's next larger element
std::vector<int>::iterator next_larger_pivot = pivot;
for (std::vector<int>::iterator i = num.end()-; i != num.begin(); --i)
{
if ( *pivot<*i )
{
next_larger_pivot = i;
break;
}
}
std::swap(*pivot, *next_larger_pivot);
// reverse the pivot's right elements
std::reverse(pivot+, num.end());
}
}
Tips:
上网搜搜Next Permutation的算法,分四步:
1. 从右往左 找左比右小的左 记为pivot
2. 从右往左 找第一个比pivot大的 记为next_larger_pivot
3. 交换pivot与next_larger_pivot所指向的元素
4. 让pivot右边的元素(不包括pivot)逆序
按照上面的算法,多测测case,找找边界条件。
注意找到pivot和next_larger_pivot之后要break退出循环。
===================================
第二次过这道题,思路已经记得比较模糊了。能做到的就是捡一遍思路,coding的过程很快。
// from right to left find first violate increase trend
int pos1 = -;
for ( int i=nums.size()-; i>; --i )
{
if ( nums[i]>nums[i-] )
{
pos1 = i-;
break;
}
}
// cout << "pos1:" << pos1 << endl;
if ( pos1==- )
{
std::reverse(nums.begin(), nums.end());
return;
}
// from right to left find the first larger than pos1
int pos2 = nums.size()-;
for ( int i=nums.size()-; i>=; --i )
{
if ( nums[i]>nums[pos1] )
{
pos2 = i;
break;
}
}
// cout << "pos2:" << pos2 << endl;
// swap pos1 pos2
std::swap(nums[pos1], nums[pos2]);
// reverse from pos1's right to end
std::reverse(nums.begin()+pos1+, nums.end());
这个算法就是个套路,多扫几遍记住就OK了。
【Next Permutation】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- Unity3D Shader性能排行
整体上,性能由高到低: Unlit,仅为纹理,光线不产生效果 VertexLit Diffuse 漫反射 Normal Mapped 法线贴图 Specular 高光 Normal Mapped Sp ...
- zabbix-3.4-服务监控
服务监控 总览 服务监控(services monitoring)旨在帮助那些想要高级(业务)基础设施的监控的人.在许多情况下,我们关注的不是底层细节,比如磁盘空间不足.CPU 负载高等.我们关注的是 ...
- SPOJ - MATSUM Matrix Summation---二维树状数组
题目链接: https://vjudge.net/problem/SPOJ-MATSUM 题目大意: 二维数组,两种操作 SET 将某点设置成x SUM 求某个区域之和 解题思路: 这里用二维树状数组 ...
- Android(java)学习笔记87:Android音视频MediaRecorder用法
1. Android语音录制可以通过 MediaRecorder 和 AudioRecorder: MediaRecorder本来是多媒体录制控件,可以同时录制视频和语音,当不指定视频源时就只录制语 ...
- c++连接mysql并提示“无法解析的外部符号 _mysql_server_init@12”解决方法&提示缺少“libmysql.dll”
课程作业要用c++连接mysql server,但是出现些小问题,经查阅资料已经解决,做一下笔记. 环境:vs2017, mysql版本是8.0.16-winx64. 设置项目属性 项目 - C ...
- STM32启动流程
启动文件主要工作: . 设置堆栈指针SP=_initial_sp . 设置PC指针=Reset_Handler . 配置系统时钟 . 配置外部SRAM用于程序变量等数据存储(可选) . 调用C库中的_ ...
- 使用VSCode搭建TypeScript开发环境 (重点)
下载TypeScript 在CMD(Windows系统)或者终端(macOS系统)中输入一下命令: npm install -g typescript 下载VSCode VSCode是我使用过最棒的编 ...
- Java时间为什么从1970-01-01 00:00:00 000开始
不仅仅是Java,几乎所有的语言的时间都是从这一刻开始算起的. 原因:java起源于UNIX系统,而UNIX认为1970年1月1日0点是时间纪元. 最初计算机操作系统是32位,而时间也是用32位表示. ...
- Oracle数据库学习(三)
6.关于null 数据库中null是一个未知数,没有任何值:进行运算时使用nvl,但是结果仍为空:在聚集函数中只有全部记录为空才会返回null. 7.insert插入 (1)单行记录插入 insert ...
- Steamroller-freecodecamp算法题目
Steamroller 1.要求 对嵌套的数组进行扁平化处理.你必须考虑到不同层级的嵌套. 2.思路 设定结果数组res 用for循环遍历arr的元素,判断是否为数组,是,则用res=res.conc ...