题目

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<2) return;std::vector<int>::iterator pivot=num.end();// find the pivot pointerfor (std::vector<int>::iterator i = num.end()-1; i != num.begin(); --i){if ( *i>*(i-1) ){pivot = i-1;break;}}// if the largest, directly reverseif ( pivot==num.end() ) {std::reverse(num.begin(), num.end());return;}// else exchange the value of pivot and it's next larger elementstd::vector<int>::iterator next_larger_pivot = pivot;for (std::vector<int>::iterator i = num.end()-1; i != num.begin(); --i){if ( *pivot<*i ){next_larger_pivot = i;break;}}std::swap(*pivot, *next_larger_pivot);// reverse the pivot's right elementsstd::reverse(pivot+1, 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 trendint pos1 = -1;for ( int i=nums.size()-1; i>0; --i ){if ( nums[i]>nums[i-1] ){pos1 = i-1;break;}}// cout << "pos1:" << pos1 << endl;if ( pos1==-1 ) {std::reverse(nums.begin(), nums.end());return;}// from right to left find the first larger than pos1int pos2 = nums.size()-1;for ( int i=nums.size()-1; i>=0; --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 endstd::reverse(nums.begin()+pos1+1, nums.end());

这个算法就是个套路,多扫几遍记住就OK了。

转载于:https://www.cnblogs.com/xbf9xbf/p/4450920.html

【Next Permutation】cpp相关推荐

  1. 【学习点滴】cpp遇到的一些疑问和积累

    目录 1.虚函数的问题 2.多种构造函数问题 3.全局变量和局部变量重名的问题: 4.有符号数溢出问题 5.自己实现c的字符串库函数 6.栈破坏问题 7.右值引用 移动构造和移动赋值 8.字符串作为h ...

  2. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /*** Definition for s ...

  3. Hdu 1072 【广搜】.cpp

    题意: 给出一个n*m的矩阵, 0 表示不可走 1 表示可走 2 表示起点 3 表示终点 4 表示可走且走到这一步可以满血 某人一开始有6滴血,走一步少一滴..到0就死了.. 可以走到4的位置满血再走 ...

  4. POJ 1904 【强连通分量】.cpp

    题意: 很久很久以前.. 有一个国王.. 他有好几个儿子.. 这些王子都喜欢上了邻国的公主.. 他们准备迎娶自己喜欢的公主中的一个.. 国王就让宰相给列一个清单.. 宰相就给了国王一个清单..上面写明 ...

  5. POJ 3422 【最大费用】.cpp

    题意: 给出一个每一格带值的矩阵 每一次只可以从左上角走到右下角 问走过k次后最多能得到多少值 P.S 走过的格子值会变成0 输入: 给出一个n 和 k 给出n*n 矩阵 思路: 因为求的是最大值 所 ...

  6. 【Divided Two】cpp

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  7. 【Visual C++】游戏开发笔记十五 游戏人工智能(一) 运动型游戏AI .

    本系列文章由zhmxy555编写,转载请注明出处.  http://blog.csdn.net/zhmxy555/article/details/7434317 作者:毛星云    邮箱: happy ...

  8. 【Android 逆向】Dalvik 函数抽取加壳 ( 类加载流程分析 | Class.cpp#findClassNoInit 函数 | DexFile.cpp#dexFindClass 函数分析 )

    文章目录 前言 一.Class.cpp#dvmDefineClass 函数分析 二.Class.cpp#findClassNoInit 函数分析 三.DexFile.cpp#dexFindClass ...

  9. 【Android 逆向】Dalvik 函数抽取加壳 ( 类加载流程分析 | native 函数查询 | dalvik_system_DexFile.cpp#defineClassNative 函数 )

    文章目录 前言 一.查询 defineClassNative 函数 二.dalvik_system_DexFile.cpp#Dalvik_dalvik_system_DexFile_defineCla ...

最新文章

  1. 蚂蚁某程序员吐槽前端招人难:一是因为要求高,二是因为招聘卷!网友却说:十万月薪也不去阿里!...
  2. 我的第二故乡 - 长沙
  3. 计算机排版基础知识,计算机排版基础知识.pdf
  4. 爬虫学习笔记(五)——网页解析工具(bs4、xpath)
  5. 用计算机控制英文,计算机控制
  6. MOXy作为您的JAX-RS JSON提供程序–服务器端
  7. 请使用C#的文件流来拷贝文件
  8. Flex(try-catch-finally)机制
  9. Mat 转 IplImage
  10. 关于解决华硕TUF B360M-PLUS GAMING在WIN10下无网卡驱动的方法
  11. 如何用命令行和carbite c++生成sis文件
  12. segno-纯Python语言的二维码和微二维码生成器
  13. phpmywind 查询结果生成csv文件并下载到本地
  14. mac软件全屏时候最顶上任务栏保留_一键整理 Mac 顶部菜单栏,这款免费工具 App 还你清爽...
  15. 两个电阻的并联与串联
  16. gym101532 2017 JUST Programming Contest 4.0
  17. 简单两步搞定小米路由新增功能-DDNS
  18. 大型项目的管理(学习)
  19. 低码框架 json-script-rule 配置说明
  20. zabbix监控EMC VNX5500等系列存储

热门文章

  1. 计算机二级考vb试题,2017计算机二级考试VB练习题及答案
  2. PHP设定美国东部时区,PHP中设置时区方法总结
  3. ie com接口 php_PHP webservie连接.net接口
  4. WPF学习笔记(7):DataGrid中数字自定义格式显示
  5. Android中的事件分发和处理
  6. 设计模式(五):命令模式
  7. C#递归遍历指定目录下文件和文件夹
  8. 设置office首字母不变大小的手段
  9. POJ 1003 Hangover
  10. 编造机中鼠标无法应用标题问题解答