[Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 31: Next Permutation
https://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,2
3,2,1 → 1,2,3
1,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的更多相关文章
- 【LeetCode】31. Next Permutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
- 【一天一道LeetCode】#31. Next Permutation
一天一道LeetCode系列 (一)题目 Implement next permutation, which rearranges numbers into the lexicographically ...
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- leetcode problem 31 -- Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode】31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode OJ 31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode】31. Next Permutation (2 solutions)
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
随机推荐
- WdatePicker日历控件使用方法
1. 跨无限级框架显示 无论你把日期控件放在哪里,你都不需要担心会被外层的iframe所遮挡进而影响客户体验,因为My97日期控件是可以跨无限级框架显示的 示例2-7 跨无限级框架演示 可无限跨越框架 ...
- SQL字符型字段按数字型字段排序实现方法(转)
由于是按字母顺序排列,所以123排在了2的前面,显然不符合我们的要求,那么怎样才能按照我们预想的数字顺序排序呢 ORDER BY `meta_value` 那么按得分排序得到的结果可能是:1101 ...
- [原创]NT系统信息察看工具 : NtInfoGuy
原文链接:[原创]NT系统信息察看工具 : NtInfoGuy 对于windows的内部,我们有太多的东西需要了解,认知.我们非凡的.从不知足的探求本性驱使我们要 拨开迷雾得见青天.太多的木马,病毒, ...
- MYSQL 二进制还原
解决方法: mysqlbinlog bin_log_file_path_and_name | mysql -uroot -p 如: mysqlbinlog E:\DB\mysql_log\mysql_ ...
- ARM 7 用户模式下禁止/使能中断的一种方法--使用软中断 for Keil MDK
最近写一个程序,需要在用户模式下关中断,但ARM 7的体系结构决定了中断必须在特权模式下才可以更改,所以想到使用ARM的软中断来实现关中断和开中断. 使用软中断,首先要有硬件指令的支持.ARM有条指令 ...
- 杭电oj 2095 & 异或^符号在C/C++中的使用
异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...
- Spring、Spring依赖注入与编码剖析Spring依赖注入的原理
Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...
- C#语言小结
数据类型--变量与常量--运算符与表达式--语句(if,for)--数组--函数--结构体 一.数据类型:(一)内建类型整型(int short long byte uint ushort ulong ...
- Java代码整理
- js——事件
焦点:使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么他就可以接收用户的输入. 我们可以通过一些方式给元素设置焦点 1.点击 2.tab 3.js 不是所有元素都能够接收焦点的.能够响应用 ...