class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
i = len(nums) - 1
if len(list(set(nums))) != 1:
#先從尾部升序結束的點
while i - 1 >= 0:
if nums[i] <= nums[i - 1]:
i = i - 1
else:
break
#如果前面還有至少一個位置
if i - 1 >= 0:
j = i - 1
t = len(nums) - 1
#從后往前找第一個大于j位置上的數(shù)
while nums[t] <= nums[j]:
t -= 1
nums[t], nums[j] = nums[j], nums[t]
a = sorted(nums[i:])
a_index = 0
#因為我不知道python分段排序的方法圃泡,于是就手動排序
#以下是對nums的排序
for index in range(i, len(nums)):
nums[index] = a[a_index]
a_index += 1
#沒有位置則sort
else:
nums.sort()
AC