# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'

31: Next Permutationhttps://oj.leetcode.com/problems/next-permutation/

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,23,2,1 → 1,2,31,1,5 → 1,5,1

===Comments by Dabay===

比如 5 9 8 4 2 1从末尾1开始往前面看,知道升序结束。在【9 8 4 2 1】中找一个正好比5大的数8,交换变成8 9 5 4 2 1。然后把 【9 5 4 2 1】反转,变成【1 2 4 5 9】。最后结果为: 8 1 2 4 5 9

'''

class Solution:    # @param num, a list of integer    # @return a list of integer    def nextPermutation(self, num):        i = len(num) - 1        while i > 0:            if num[i-1] >= num[i]:                i -= 1                continue            j = i            while j < len(num):                if num[j] > num[i-1]:                    j += 1                    continue                break            num[i-1], num[j-1] = num[j-1], num[i-1]            tails = num[i:]            tails.reverse()            return num[:i] + tails        else:            num.reverse()            return num

def main():    s = Solution()    nums = [5, 1, 1]    print s.nextPermutation(nums)

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)

[Leetcode][Python]31: Next Permutation的更多相关文章

  1. 【一天一道LeetCode】#31. Next Permutation

    一天一道LeetCode系列 (一)题目 Implement next permutation, which rearranges numbers into the lexicographically ...

  2. LeetCode 【31. Next Permutation】

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

  3. leetcode problem 31 -- Next Permutation

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

  4. 【LeetCode】31. Next Permutation

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

  5. LeetCode OJ 31. Next Permutation

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

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

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

  7. LeetCode - 31. Next Permutation

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

  8. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  9. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. 配置IIS的通配符应用程序映射

    使用IIS 6架设网站,如果要使用伪静态的功能,可能需要设置“通配符应用程序映射(执行顺序)”. 在Windows Server 2012 r2 的IIS 8中,对应的是添加设置“通配符脚本映射”,参 ...

  2. word20161225

    Waiting for Call / 等待呼叫 wallpaper / 墙纸 WAN, wide area network / 广域网 warning level / 警告级别 Web folder ...

  3. 微信WeixinJSBridge API(屏蔽右上角按钮等)

    [声明]:我这份纯属于备份,为了自己将来用起来方便: [相关链接]:http://www.2cto.com/weixin/201511/451592.html(好不好用完全看这个文档的作者了) [感谢 ...

  4. ng-selected ng-change

    <!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Cont ...

  5. Flash Builder 调试器无法连接到正在运行的应用程序(57%)

    Flash Builder 调试器无法连接到正在运行的应用程序(57%),可能原因:     1,flashplayer不是debug版.     2,调试器(用debug版flashplayer随便 ...

  6. 字符识别(模板匹配&amp;BP神经网络训练)

    http://blog.csdn.net/zhang11wu4/article/details/7585632

  7. IT人的自我导向型学习:学习的1个理念和2个心态

    本文更新版本已挪至  http://www.zhoujingen.cn/blog/2484.html ----------------------------- 写这一个系列之前,我定位是与高效学习有 ...

  8. PHP实例学习之&mdash;&mdash;&mdash;&mdash;MVC架构模式分析与设计

      写博客,不容易,你们的评论和转载,就是我的动力, 但请注明出处,隔壁老王的开发园:http://www.cnblogs.com/titibili 2016年1月31日 18:53 作者:Ygirl ...

  9. Maximum Subsequence Sum(接上篇)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  10. SQL with as

    姓名 课程 分数 张三 语文 张三 数学 张三 物理 李四 语文 李四 数学 李四 物理 先看下面一个嵌套的查询语句 ) 上面的查询语句使用了一个子查询.虽然这条SQL语句并不复杂,但如果嵌套的层次过 ...