class Solution:
# @param num, a list of integer
# @return a list of integer
def nextPermutation(self, num):
k=len(num)
if k==1:
return num
left=-1
for i in range(k-2,-1,-1):
if num[i]<num[i+1]:
left=i
break
for i in range(k-1,-1,-1):
if num[i]>num[left]:
right=i
break
if left>=0:
num[left],num[right]=num[right],num[left]
l=left+1
else:
l=0
while l<k-1:
num[l],num[k-1]=num[k-1],num[l]
l+=1
k-=1
return num
Friday, September 12, 2014
Leetcode: Next Permutation @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment