题目

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的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. 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~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

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

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. Unicode字符集

    Unicode字符集的出现是为了弥补ASCII码只能表示128个字符的限制.在实际应用中,如若我们想显示汉字或日文等等,显然使用ASCII是不可能的.Unicode占用了两个字节,即16位,能表示的字 ...

  2. [转]linux远程登入不需要密码

    如何通过一台linux ssh远程其他linux服务器时,不要输入密码,可以自动登入.提高远程效率,不用记忆各台服务器的密码. 工具/原料   ssh,ssh-keygen,scp 方法/步骤     ...

  3. Callable的简单使用

    说起java的线程操作,都会想到Thread和Runable这两个, 这两个类可以实现异步和同步. 在大多数的java开发中, 这两个都是实现异步的线程来使用, 但是现在考虑一种情况: 发出一条线程, ...

  4. Spring @Autowired使用介绍

    参考博客: https://blog.csdn.net/u013412772/article/details/73741710 引用文章地址: https://my.oschina.net/Helio ...

  5. 合理设置apache的最大连接数

    手头有一个网站在线人数增多,访问时很慢.初步认为是服务器资源不足了,但经反复测试,一旦连接上,不断点击同一个页面上不同的链接,都能迅速打开,这种现象就是说明apache最大连接数已经满了,新的访客只能 ...

  6. 红象云腾CRH 一键部署大数据平台

    平台: arm 类型: ARM 模板 软件包: azkaban hadoop 2.6 hbase hive kafka spark zeppelin azkaban basic software bi ...

  7. No module named 'revoscalepy'问题解决

    SqlServer2017开始支持Python,前段时间体验了下,按照微软的入门例子操作的:https://microsoft.github.io/sql-ml-tutorials/python/re ...

  8. Java JDBC链接Oracle数据库

    package com.test.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import ja ...

  9. 性能调优--大事务与Alwayson 之间的关系

    最近性能调优的事比较多,所以摘一些比较有特点的 案例分享下. 业务系统用的是sql server 2016 ,搭建的ALWAYSON 两节点的 群集,今天早上突然辅助 副本的只读库出现大量的等待导致系 ...

  10. html5标准

    1.<!DOCTYPE html> html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5 浅谈:htm ...