描述:

  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. json具体解释

    <span style="font-size:12px;">function testJson() { var jsonData = { "firstName ...

  2. SVN 权限配置具体说明

    svnserve权限配置 分配权限时.文件夹应该应该遵从从大到小,权限应该从小到大的规则 即:文件夹从根文件夹開始,权限从没有权限(为空就可以)到可写再到可读写. 提示:文件夹的訪问权限既能够分配给组 ...

  3. BaseAdapter 注意的关键点!

    BaseAdapter  我们一般就是继承然后重写自定义,然后listview  set进去即可!  数据改变的时候,我们习惯这样: public void update(List list) {   ...

  4. Eclipse下运行拷贝的项目,更改项目名后报404

    右键项目->Properties->Web Project Settings 将“Context root”改为你的项目名称

  5. TreeSet类的排序问题

     http://www.cnblogs.com/lixiaolun/archive/2012/12/25/2832775.html TreeSet支持两种排序方法:自然排序和定制排序.TreeSet默 ...

  6. poj1936--暴力解法

    求s1是否是s2的子串. 分析: 例如sequence 和 subsequence . 从头开始比较,s1[0]与s2[0]相同,那么它们下标都可+1,接着比较s1[1]和s2[1],不相同:这时应该 ...

  7. Manacher 算法

    Manacher算法用于求回文子串,它的复杂度为O(n). 这个算法有一个很巧妙的地方,它把奇数的回文串和偶数的回文串统一起来考虑了.在相邻的两个字符之间加进一个分隔符 '#' ,串的首尾也要加. 原 ...

  8. 树 -- AVL树

    前言 通过之前对二叉查找树的讨论,我们知道在给定节点数目的情况下,二叉树的高度越低,查找所用时间也就越短. 在讨论红黑树的时候,我们说过红黑树并非完全"平衡"的二叉树,只是近似&q ...

  9. linux杂记(三)linux指令介绍

    [root@linux ~]# command [-options] parameter1 parameter2 说明: 最左边的root显示的是[目前使用者的账号],而@之后接的是linux即[主机 ...

  10. ExtJS4.2 Ext.grid.panel Store更改后刷新表格

    //////////////////////// // Prepare store //////////////////////// // prepare fields and columns var ...