[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 ...
随机推荐
- SQL Server 数据库状态选项
选项 1. single_user(单用户),multi_user(多用户),restricted_user(受限用户); 描述数据库的用户访问属性,它们互斥,设置其中任何一个选项就会取消对其它选项的 ...
- oc block基本使用
// // main.m // block基本使用 // // Created by Ymmmsick on 15/7/21. // Copyright (c) 2015年 Ymmmsick. All ...
- Log4j的应用实例(转)
转自:http://www.cnblogs.com/eflylab/archive/2007/01/12/618080.html 在Log4J使用笔记中没有怎么写实例,那么在这篇中我将Log4j的一个 ...
- Hot-Bar 軟板設計注意事項
Hot-Bar reflow (熔錫熱壓焊接),其最只要功能,就是利用熱壓頭熔融已經印刷於電子印刷電路(PCB)上的錫膏,藉以連接兩個各自獨立的電子零件,最常見到的是將軟排線(FPB)焊接於電子印刷電 ...
- HttpContext.Current.Cache 过期时间
原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...
- Java - 字符串和Unicode互转 - 解析小米pm.min.js
小米JS地址: http://p.www.xiaomi.com/zt/20130313/huodong/pm.min.js 上面这个JS是小米抢手机页面的代码.和抢手机有直接关联.. 虽然我3次都没抢 ...
- 支持https的压力测试工具
支持https的压力测试工具 测试了linux下的几种压力测试工具,发现有些不支持https,先简单总结如下: 一.apache的ab工具 /home/webadm/bin/ab -c 50 -n 1 ...
- C/C++源代码的Include依赖关系图
前一篇博文中我曾仔细介绍过如何查看C/C++代码的依赖项关系图,在这篇文章中我将会介绍如何使用Visualization and Modeling Feature Pack 工具包,查看C/C++源代 ...
- Hibernate 、多表关联映射-组件关联映射(component)
组件关联映射可以将一些简小的数据与主题放在一个表中,例如firstName 和LastName这两个结合在一起可以组成一个名字,但是再分别将这两个再建一个表就不太合适了,这个时候可以用到组件关联映射: ...
- 重写equal要重写 hashCode的原因
public class Test { public static void main(String[] args) { Person person1 = new Person(); person1. ...