classSolution:# @param num : a list of integer# @return : a list of integerdefnextPermutation(self,num):# write your code hereifnum==Noneorlen(num)<2:returnnumend=len(num)-1front=end-1# find the element who breaks the rulewhilefront>=0:ifnum[front]<num[end]:breakelse:front-=1end-=1iffront==-1:num.sort()else:# find the smallest element(element>num[front])end=len(num)-1whileend>front:ifnum[end]>num[front]:breakelse:end-=1# swap the front, end num[front],num[end]=num[end],num[front]num[front+1:]=sorted(num[front+1:])returnnum