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

题意:用现有的数字排序,找出比现在数字大的最小的数,如果没有的话,重新排序为最小值

思路:从低位往高位遍历,如果相邻高位比现位置值大的话,break

 class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
flag = 0
l = len(nums)
for i in range(l-1,0,-1):
if nums[i-1]<nums[i]:
flag = 1
break
if 1 == flag:
tmp = nums[i:]
tmp.sort()
nums[i:]=tmp
for k in range(i,l):
if nums[k]>nums[i-1]:
tmp = nums[k]
nums[k] = nums[i-1]
nums[i-1] = tmp
break
else:
nums.reverse()

【LeetCode】31. Next Permutation的更多相关文章

  1. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  2. 【LeetCode】31. Next Permutation (2 solutions)

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

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

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

  4. 【LeetCode】031. Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  5. 【leetcode】266. Palindrome Permutation

    原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...

  6. 【leetcode】1053. Previous Permutation With One Swap

    题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...

  7. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  8. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. 数据访问层的改进以及测试DOM的发布

    数据访问层的改进以及测试DOM的发布 在上一篇我们在宏观概要上对DAL层进行了封装与抽象.我们的目的主要有两个:第一,解除BLL层对DAL层的依赖,这一点我们通过定义接口做到了:第二,使我们的DAL层 ...

  2. 详解SpringMVC请求的时候是如何找到正确的Controller

    详解SpringMVC请求的时候是如何找到正确的Controller[附带源码分析] 目录 前言 源码分析 重要接口介绍 SpringMVC初始化的时候做了什么 HandlerExecutionCha ...

  3. NLP startup material

    The Association for Computational Linguistics(ACL,URL:http://aclweb.org/) Computational Linguistics( ...

  4. JAVA修饰符类型(转帖)

    JAVA修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的任何类均可以访问:protected的类.类属变量及方法,包内的 ...

  5. VIJOS1107 求树的最长链

    vijos1107环游大同80天 学习了一下求树的最长链的方法 最简单的思路就是两次dfs 两次dfs分别有什么用呢? 第一次dfs,求出某个任意的点能到达的最远的点 第二次dfs,从所搜到的最远的点 ...

  6. 回调函数 use

    $info["fulltext"] = preg_replace_callback( $search2, function($matches) use ($search, $uni ...

  7. 安装xdebug后var_dump输出没有格式化的问题

    在ubuntu10.04下面配置Apache2.2 + PHP5.3的开发环境,顺便装了一个xdebug扩展方便调试代码. 但是环境配置好了之后却发现xdebug加载成功了但是var_dump输出的内 ...

  8. [转]SVN操作手册

    [转]SVN操作手册 2012-04-28 11:26 by NewSea, 2495 阅读, 0 评论, 收藏, 编辑 原文: http://hi.baidu.com/caiqiupeng/blog ...

  9. 关于AngularJS的分享

    去年下半年在部门内部做的一个关于AngularJS的分享,放在这里,当作一个记录. 点击下载

  10. Go学习笔记 - 使用jsonrpc进行远程访问

    Go学习笔记 - 使用jsonrpc进行远程访问 JSON-RPC JSON-RPC是一个轻量级的远程调用协议,简单易用. 请求数据体: { "method": "get ...