【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,2
3,2,1
→ 1,2,3
1,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 ...
随机推荐
- iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择拷贝到工程中,(拖入的文件夹是蓝色 ...
- 六、C++离散傅里叶逆变换
C++离散傅里叶逆变换 一.序言: 该教程承接上文的离散傅里叶变换,用于进行离散傅里叶逆变换. 二.设计目标 对复数数组进行离散傅里叶逆变换,并生成可供使用的图像类. 三.详细步骤 输入:经傅里叶变换 ...
- 【BZOJ2049】[SDOI2008] Cave 洞穴勘测(LCT维护连通性)
点此看题面 大致题意: 有\(n\)个洞穴,\(3\)种操作:连一条边,删一条边,询问两点是否联通. \(LCT\)维护连通性 这道题应该是\(LCT\)动态维护连通性的一道模板题. 考虑将\(x\) ...
- 【BZOJ4458】GTY的OJ(树上超级钢琴)
点此看题面 大致题意: 给你一棵树,让你求出每一个节点向上的长度在\([l,r]\)范围内的路径权值和最大的\(m\)条路径的权值总和. 关于此题的数列版本 此题的数列版本,就是比较著名的[BZOJ2 ...
- 复习C++_基础、函数、数组、字符串
程序的开发过程 程序 源程序:用源语言写的,有待翻译的程序: 目标程序:源程序通过翻译程序加工以后生成的机器语言程序: 可执行程序:连接目标程序以及库中的某些文件,生成的一个可执行文件,例如Windo ...
- python_49_三种编程方式及面向过程与面向函数区别.py
''' 三种编程方式:1.面向对象 (类:class)2.面向过程 (过程:def)3.函数式编程(函数:def) 编程语言中函数的定义:函数是逻辑结构化和过程化的一种编程方法 过程与函数的区别,过程 ...
- NUMA的原理与局限
为什么要有NUMA 在NUMA架构出现前,CPU欢快的朝着频率越来越高的方向发展.受到物理极限的挑战,又转为核数越来越多的方向发展.如果每个core的工作性质 都是share-nothing(类似于m ...
- Javascript显示提示信息加样式
#region JS提示============================================ /// <summary> /// 添加编辑删除提示 /// </s ...
- http 调用错误处理
1. http code 在使用Nginx时,经常会碰到502 Bad Gateway和504 Gateway Time-out错误,下面以Nginx+PHP-FPM来分析下这两种常见错误的原因和解决 ...
- 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包
今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, 在网上搜了一下,这个问题是因为web项目没有添加服务器导致的. 配置tomec ...