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. psql: FATAL: role “postgres” does not exist 解决方案

    当时想做的事情,是运行一个创建数据库的脚本.找到的解决方案差不多和下面这个链接相同. http://stackoverflow.com/questions/15301826/psql-fatal-ro ...

  2. JS实现等比例缩放图片

    JS实现等比例缩放图片 2014-01-19 21:57 by 龙恩0707, 40 阅读, 0 评论, 收藏, 编辑 JS实现等比例缩放图片 有时候我们前端页面只有500×500像素的宽和高的布局, ...

  3. Linq to XML的练习

    假如有以下XML: <?xml version="1.0" encoding="utf-8" ?> - <export> - <p ...

  4. c#多选下拉框(ComboBox)

    代码如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawi ...

  5. [转] iOS ABI Function Call Guide

    source: apple ARMv6 Function Calling Conventions When functions (routines) call other functions (sub ...

  6. 用django搭建一个简易blog系统(翻译)(三)

    06. Connecting the Django admin to the blog app Django 本身就带有一个应用叫作Admin,而且它是一个很好的工具 在这一部分,我们将要激活admi ...

  7. Hook SSDT中NtCreateProcessEx

    #ifdef __cplusplus extern "C" { #endif #include <ntddk.h> #ifdef __cplusplus } #endi ...

  8. ASP.NET MVC学习笔记-----Filter2

    ASP.NET MVC学习笔记-----Filter(2) 接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用 ...

  9. MongoDB的一些用法(转藏)

    MongoDB是目前工作中经常使用到的NoSQL数据库. 本博客只记录相关理论知识和技巧,涉及到实践的部分都会单开Blog来记录实践过程. ------------------------------ ...

  10. C#5.0支持的await格式

    C#5.0支持的await格式 C#5.0引入了编译器支持的 async 和 await 关键字,这就为开发者提供了使用同步思想写异步代码的方便. 但是有些传统函数仅提供了异步回调实现,如何对其封装, ...