[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 ...
随机推荐
- #CI的MVC实现
CI的MVC实现 CI被标榜为一款简单易用的框架,经过一段时间的了解后,它的小而精给让我印象深刻.麻雀虽小五脏俱全,一个框架产品包含太多的特性,这篇文章就说说CI中是如何实现MVC的? 执行流程 根据 ...
- intial-scale=1的含义
meta name="viewport" content="width=device-width,initial-scale=1.0" 解释 <meta ...
- Linux05--Shell程序设计01
1.Shell脚本介绍 基本介绍: shell脚本是一个可执行的纯文本文件,由多个shell命令组成. 命令的执行是从上而下,从左而右的分析和执行 命令,参数间的多个空白也会被忽略 #是注释 #!用于 ...
- 客户端持久化解决方案: Web SQL
客户端持久化解决方案: Web SQL Web SQL 提供了一组使用 SQL 操作客户端数据库的 APIs, 不是 HTML5 规范的一部分,是一个独立的规范. 核心方法 openDatabase: ...
- centos 6.5安装GitLab全过程和问题记录
GitLab,是一个使用 Ruby on Rails 开发的开源应用程序,与Github类似,能够浏览源代码,管理缺陷和注释,非常适合在团队内部使用. 官方只提供了Debian/Ubuntu系统下的安 ...
- 普林斯顿大学算法课 Algorithm Part I Week 3 自我总结
要熟练掌握比较器Comparator public final Comparator<T> MY_COMPARATOR = new myComparator(); //定义比较器 .... ...
- Swift应用开源项目推荐
1. 风靡全球的2048 2014年出现了不少虐心的小游戏,除了名声大噪的Flappy Bird外,最风靡的应该就是2048了.一个看似简单的数字叠加游戏,却让玩的人根本停不下来,朋友圈还一度被晒分数 ...
- for应用
应用:迭代法,穷举法.一.迭代法:有一定规律. 每次循环都是从上次运算结果中获得数据,本次运算的结果都是要为下次运算做准备.例:1.100以内所有数的和.2.求阶乘3.求年龄.4.折纸.5.棋盘放粮食 ...
- C#学习基础总结
概念:.net与c#.net/dontnet:一般指.net framework框架,一种平台,一种技术c#(charp):一种编程语言,可以开发基于.net的应用. *java既是一种技术又是一种编 ...
- .net通用权限框架B/S (三)--MODEL层(1)
1.新建c#类库 2.安装配置好entity frame5 3.新建的类库项目上右键"添加--新建项",选择AOD.NET实体数据模型 4.设置数据库连接, 5.选择建好的表 6. ...