[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 ...
随机推荐
- jquery插件的编写
今天尝试了一下自己编写插件.最简单的jquery效果,返回顶部的按钮. 增加多个全局函数 添加多个全局函数,可采用如下定义: Java代码 jQuery.foo = function() { aler ...
- hdu 2509 Be the Winner 博弈
题目链接 有n堆苹果, 对于其中的每一堆的x个苹果, 它是放在一条线上的. 你每次可以对一堆苹果进行操作, 可以取y个, 1<=y<=x. 然后如果你是取的一条线上中间的苹果, 那么这一堆 ...
- 正则语言(转的 大额_skylar )
备注:正则表达式真的很头疼,收集起来,用起来很方便的. 常用的元字符 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或 ...
- Pig On Mac
Install 首先是 Mac OS 下的安装 1 2 export JAVA_HOME=$(/usr/libexec/java_home) brew install pig Run Pig 运行分为 ...
- Build A Micro Team
最近这两周一直在忙,忙到忘记回QQ和微信的程度,甚至有两天下班回来直接躺平,虽说忙碌但是也收获了不少. Start 说起来有趣,最近在忙的这个项目最初是公司设立的用于工程师在业余时间开发的projec ...
- UITableViewCell性能优化
5.UITableViewCell性能优化 > 定义一个循环利用标识 static NSString *ID = @"C1"; > 从缓存池中取出可循环利用的cell ...
- VC维度
由vc bound可以知道: $P(\exists h\in H~s.t~|E_{in}(h)-E_{out}(h)|>\epsilon)\\ \leq 4M_H(2N)exp(-\frac{ ...
- Hoeffding连接到机器学习
统计学场景: 一个罐子中有红球和绿球,红球比例$v$未知,数量未知,如何得到红球比例?方法---随机抽样N个球,在其中红球占比为$u$ 由hoeffding可以知道:$P(|u-v|>\epsi ...
- C语言对象化编程
以下为一个引子: C中struct的函数实现,只能用函数指针成员. C结构体内不能有函数的代码,但可以有函数的指针. C/C code Code highlighting produced by Ac ...
- leftpad填充函数;
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...