描述:

  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. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

  2. ACCESS 里面的坑

    在vs2005中用sql语句访问access数据库出现replace函数未定义错误,后来查询得知,在不能用replace访问access. 问题:   1.为什么以前运行正常的Access数据库,搬到 ...

  3. JS学习笔记(四)常用对象

    Error // 语法 throw new Error("消息"); 类似于C#中的Exception对象 // alert(num); try { throw new Error ...

  4. 【转】使用Navicat for Oracle新建表空间、用户及权限赋予

    首先.我们来新建一个表空间.打开Navicat for Oracle,输入相关的的连接信息.如下图: 填入正确的信息,连接后.我们点击面板上的“其他”下的选项“表空间”,如下图: 进入表空间的界面,我 ...

  5. ASP.NET动态引用WebService接口

    尊重原著作:本文转载自http://www.mhzg.net/a/20124/20124912180589.html 有经验的朋友都知道,通常我们在引用webservice的时候,是在项目中就添加了引 ...

  6. linux循环递归设置权限

    这里给出一个循环递归得到对文件夹和文件分别有效的设置方法: find /path -type f -exec chmod 644 {} \; #对目录和子目录里的文件 find /path -type ...

  7. 用一条sql查出总长和状态为2是长度

    查询同一张表里同一个字段值的和以及状态是2(或1)时,该字段值的和

  8. JQuery使用on绑定动态生成元素时碰到的问题

    今天在写界面时,希望对一些由JS代码动态添加的html标签进行事件绑定,使用了jquery 1.6+才提供的on函数.我的JQ版本是1.9.2. 以下这段代码是动态生成的. <div class ...

  9. ios 应用程序之间的跳转(内置程序的实现)

    ios 应用程序之间的跳转(内置程序的实现) 一个程序若要跳到另一个程序.需要在目标程序的plist文件里面修改: 打开info.plist,添加一项URL types 展开URLtypes,再展开I ...

  10. CSS层

    在使用元素定位时,从可视角度讲,不可避免地会发生两个元素试图同时出现于同一位置的情况.显示其中一个就会覆盖另外一个. 如果将网页的二维空间延伸到三维空间,就会解决上述元素覆盖问题. 在坐标系中,通过增 ...