# coding=utf-8
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
# Definition for singly-linked list.
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
1.left=0 right=0 middle=0
2.for i in range(len(nums))[::-1]:
if :升序
pass
else:
left=i
middle=i+1
break
3.for m in range(left,len(nums)):
if nums[m]>nums[i]:
right=m
break
else:
pass
4.exchange [left],[right]
5.倒置middle后边的
"""
left = 0
right = 0
middle = 0
for i in range(len(nums)-1)[::-1]:
if nums[i]>=nums[i+1]:
pass
else :
left = i
middle = i + 1
break
for m in range(middle, len(nums))[::-1]:
if nums[m]>nums[left]:
right=m
break
else:
pass
print left,middle,right
temp=nums[left]
nums[left]=nums[right]
nums[right]=temp
for n in range(middle,(middle+len(nums))/2):
temps=nums[n]
k=middle + len(nums) - 1 - n
nums[n]=nums[k]
nums[k]=temps
这里怎么说,后来为了空间开销不想记middle,然后发现直接用left+1替代middle当数组处于自然有序的时候(比如[3,2,1]),left+1=1,然后排序会漏掉nums[0]真是错错得对了