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

 class Solution:

     def nextPermutation(self, a):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
def swap(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp def reverces(a, i):
for k in range(int((len(a) - i) / 2)):
swap(a, i + k, len(a) - k - 1) # 后找
i = len(a) - 2
while(i>=0 and a[i + 1] <= a[i]):
i -= 1 # 小大
if(i >= 0):
j = len(a) - 1
while j >= 0 and a[j] <= a[i]:
j -= 1
# 交换
swap(a, i, j)
# 反转
reverces(a, i+1)

31. Next Permutation (下一个全排列)的更多相关文章

  1. [LeetCode] 31. Next Permutation 下一个排列

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

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

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

  3. [leetcode]31. Next Permutation下一个排列

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

  4. LeetCode 31 Next Permutation(下一个全排列)

    题目链接: https://leetcode.com/problems/next-permutation/?tab=Description   Problem :寻找给定int数组的下一个全排列(要求 ...

  5. NextPermutation,寻找下一个全排列

    问题描述:给定一个数组是一个全排列,寻找下一个全排列.例如123->132, 321->123, 115->151. 算法分析:从后往前寻找顺序,找到后从往前寻找第一个大于当前元素, ...

  6. [LeetCode] Next Permutation 下一个排列

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

  7. Next Permutation 下一个排列

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

  8. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  9. 【LeetCode每天一题】Next Permutation(下一个排列)

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

随机推荐

  1. ThinkPHP项目笔记之MVC篇

    题记:网上关于ThinkPHP的介绍,不计其数,有文档,示例,代码片段以及其他等.毕竟自己掌握的,才是自己的. 所以,趁着做的项目(当然用的是thinkphp框架)的余热,奋笔疾书,一个人的理解与拙笔 ...

  2. hdu 1180:诡异的楼梯(BFS广搜)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  3. hdu 1232:畅通工程(数据结构,树,并查集)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. 设置两个WdatePicker的开始时间小于结束时间,结束时间大于开始时间

    contract_start_date_id为开始时间ID contract_end_date_id为结束时间ID $("#contract_start_date_id").bin ...

  5. WPF 附加事件

    在WPF中有许多控件有他们自己的特殊的事件.按钮就是一个例子——它添加了 Click 事件,而其他任何类都没有定义该事件. 这回导致两难的境地.假设在 StackPanel 面板中包装了一堆按钮,并且 ...

  6. windows 系统重装之后怎么恢复oracle数据库

    今天单位的服务器系统进不去了,重做了系统,有重要的oracle数据,经理让我恢复一下oracle数据,试着尝试了一下 1.首先,将原来的ORACLE文件夹改名,原来的路径是D:/oracle.我暂时改 ...

  7. TreeSet排序,存储自己定义对象,自己定义比較器演示样例

    Set:无序.不能够反复元素. |--HashSet:数据结构是哈希表.线程是非同步的. 保证元素唯一性的原理:推断元素的hashCode值是否同样. 假设同样,还会继续推断元素的equals方法.是 ...

  8. Android之布局属性归纳

    第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_centerVertical 垂直居中android:layou ...

  9. requireJs官方使用教程(转)

    原文地址:http://www.requirejs.cn § 1.使用 § 1.1 加载 JavaScript 文件 RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script ...

  10. absolute float 比较分析

    同一:absolute-float 都脱离了文档流,也就是默认情况下,父盒子计算高度不包括 absolute/float 的元素 下例: 两个 parentDiv,背景色:深海蓝;边框:1px红色实线 ...