描述:

  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

字典序:

  对于数字1、2、3......n的排列,不同排列的先后关系是从左到右逐个比较对应的数字的先后来决定的。例如对于5个数字的排列 12354和12345,排列12345在前,排列12354在后。按照这样的规定,5个数字的所有的排列中最前面的是12345,最后面的是 54321。

  那么1234的全排列从小到大的顺序也就是字典序的顺序,依次如下:

    1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321

  产生字典序下一个的非递归算法:

    设P是[1,n]的一个全排列,P=P1P2...Pn=P1P2...Pj-1PjPj+1...Pk-1PkPk+1...Pn

    寻找j=max{i|Pi<Pi+1},k=max{i|Pi>Pj},swap Pj和Pk,reverse Pj+1...Pn,得到的P‘=P1P2...Pj-1PkPn...Pk+1PjPk-1...Pj+1,即为P的字典序的下一个排列,当然这里指的是等长度的。

代码:

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int j=-,k=,temp,left,right;
//j=max{i|pi<pi+1}
for( int i=nums.size()-;i>=;i-- ){
if( nums[i]<nums[i+] ){
j=i;break;
}
}
if( j==- ){//3 2 1
sort(nums.begin(),nums.end());
}else{
//k=max{i|pi>pj}
for( int i=nums.size()-;i>=;i-- ){
if( nums[i]>nums[j] ){
k=i;break;
}
}
//swap pj pk
temp=nums[j];nums[j]=nums[k];nums[k]=temp;
//reverse pj+1 pn
left=j+;right=nums.size()-;
while( left<right ){
temp=nums[left];nums[left]=nums[right];nums[right]=temp;
left++;right--;
}
}
}
};

leetcode 31. Next Permutation(字典序的下一个)的更多相关文章

  1. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  2. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  3. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  4. [LeetCode] 556. Next Greater Element III 下一个较大的元素 III

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  5. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

  6. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  7. [LeetCode] 503. Next Greater Element II 下一个较大的元素 II

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  8. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. LeetCode 31. Next Permutation (下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. 3种SQL语句分页写法

    在开发中经常会使用到数据分页查询,一般的分页可以直接用SQL语句分页,当然也可以把分页写在存储过程里,下面是三种比较常用的SQL语句分页方法,下面以每页5条数据,查询第3页为例子: 第一种:使用not ...

  2. Response.Redirec方法传递汉字出现乱码

    解决方法: //传参数时Response.Redirect("a.aspx?name"+Server.UrlEncode("我的名字")); //接收参数时 S ...

  3. MySQL常用指令

    1.win下启动MySQL  命令行下输入: mysql –h localhost –u root -p / mysql -uroot -p 2.MySql下建表 输入命令 show database ...

  4. 自定义配置文件的使用(web.config/app.config)

    以下非原创作品,但都是自己看过理解并写过,记录下来,以便之后项目的使用或其它用途. (1)只需要简单配置单一属性值: <configuration> <configSections& ...

  5. 从Quartz时间设置问题说起

    已经好久没有来写点啥了,原因有很多,不过最主要的还是自己很懒很懒,今天终于意识到问题的严重性了.所以就来了.今天的这个问题也是前不久刚刚遇到的问题.先不啰嗦,说重点了. 一.问题描述 定时任务项目发布 ...

  6. CCF计算机认证注意事项

    1,同一变量只使用一次,你是使用同名的局部变量. 2,if()条件语句里面再不要使用单一的if()条件语句. 这应该都是他们系统的bug

  7. getActionBar().setTitle(); Java.lang.NullPoint异常解决方案

    getActionBar().setTitle(); Java.lang.NullPoint异常解决方案,是由于低版本不支持直接获取的缘故,修改方案: try changing your theme ...

  8. Umbraco扩展开发

    国内Umbraco方面的资料很少,搜集到一些国外的优秀项目或插件.记录下来,便于日后使用: backoffice:https://github.com/TimGeyssens 后台扩展UI,可以在这里 ...

  9. 图文:TF卡和SD卡的区别及什么是TF卡?什么是SD卡

    小型存储设备凭借低廉的价格.多样化的品种.实用等特性大量充斥在大家身边,比如智能手机手机上.数码照相机上.游戏机上(一般是掌机)等都小型电子设备都频繁的使用到这种统称为SD的产品,比如TF卡和SD卡( ...

  10. 继承CWnd自绘按钮

    头文件: //头文件 #pragma once // CLhsButton #define MYWM_BTN_CLICK WM_USER+3001 //关闭按钮单击响应 //tab按钮的状态 enum ...