[leetcode]Next Permutation @ Python
原题地址: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
解题思路:输出字典序中的下一个排列。比如123生成的全排列是:123,132,213,231,312,321。那么321的next permutation是123。下面这种算法据说是STL中的经典算法。在当前序列中,从尾端往前寻找两个相邻升序元素,升序元素对中的前一个标记为partition。然后再从尾端寻找另一个大于partition的元素,并与partition指向的元素交换,然后将partition后的元素(不包括partition指向的元素)逆序排列。比如14532,那么升序对为45,partition指向4,由于partition之后除了5没有比4大的数,所以45交换为54,即15432,然后将partition之后的元素逆序排列,即432排列为234,则最后输出的next permutation为15234。确实很巧妙。
代码:
class Solution:
# @param num, a list of integer
# @return a list of integer
def nextPermutation(self, num):
if len(num) <= 1: return num
partition = -1
for i in range(len(num)-2, -1, -1):
if num[i] < num[i+1]:
partition = i
break
if partition == -1:
num.reverse()
return num
else:
for i in range(len(num)-1, partition, -1):
if num[i] > num[partition]:
num[i],num[partition] = num[partition],num[i]
break
left = partition+1; right = len(num)-1
while left < right:
num[left],num[right] = num[right],num[left]
left+=1; right-=1
return num
改进一点:
class Solution:
# @param num, a list of integer
# @return a list of integer
def nextPermutation(self, num):
if len(num) <= 1: return num
partition = -1
for i in range(len(num)-2, -1, -1):
if num[i] < num[i+1]:
partition = i
break
if partition == -1:
num.reverse()
return num
else:
for i in range(len(num)-1, partition, -1):
if num[i] > num[partition]:
num[i],num[partition] = num[partition],num[i]
break
num[partition+1:len(num)]=num[partition+1:len(num)][::-1]
return num
[leetcode]Next Permutation @ Python的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode]题解(python):031-Next Permutation
题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges nu ...
- [LeetCode]题解(python):060-Permutation Sequence
题目来源 https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total of n! ...
- [LeetCode] 60. Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] 567. Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
随机推荐
- [Android]对BaseAdapter中ViewHolder编写简化(转)
来自博客:http://www.cnblogs.com/tiantianbyconan/p/3642849.html 在Android项目中,经常都会用到ListView这个控件,而相应的Adapte ...
- BZOJ4275 : [ONTAK2015]Badania naukowe
设f[i][j]为a[1..i]与b[1..j]的LCS,g[i][j]为a[i..n]与b[j..m]的LCS. 若C为0,则ans=f[n][m]. 否则求出d[i]=a中从i开始往右匹配上c串的 ...
- JS funtion()中URL不跳转后台action问题
JS funtion()中URL不跳转后台action问题 今天遇到一个百思不得其解的问题,到现在解决了,但是仍然不知道所以然(估计是因为域名不一致导致的),记录一下 $.get(actionUrl, ...
- Nginx担当WebSockets代理
Nginx担当WebSockets代理 英文原文:http://nginx.com/blog/websocket-nginx/ 作者:chszs,转载需注明. 博客主页:http://blog.csd ...
- WICED SDK 3.3.1
7/20/2015 UPDATE: After installing the IDE you may not see all the APPs. Press F5 in Eclipse to ref ...
- EasyNetQ介绍
EasyNetQ 是一个容易使用,坚固的,针对RabbitMQ的 .NET API. 假如你尽可能快的想去安装和运行RabbitMQ,请去看入门指南.EasyNetQ是为了提供一个尽可能简洁的适用与R ...
- 如何在socket编程的Tcp连接中实现心跳协议
from http://blog.csdn.net/nyist327/article/details/39586203 心跳包的发送,通常有两种技术方法1:应用层自己实现的心跳包 由应用程序自己发送心 ...
- java 对mongodb的操作
java 对mongodb的操作 1.1连单台mongodb Mongo mg = newMongo();//默认连本机127.0.0.1 端口为27017 Mongo mg = newMongo( ...
- Delphi 判断时间是否合法 -IsValidDateTime、IsValidDate、IsValidTime、IsValidDateDay
DateUtils.IsValidDateTimeDateUtils.IsValidDateDateUtils.IsValidTimeDateUtils.IsValidDateDayDateUtils ...
- CentOS 安装 nexus (maven 私服)
原文:https://www.sunjianhua.cn/archives/centos-nexus.html 1.下载 wget http://download.sonatype.com/nexus ...