Sharing my short and simple Python solution


  • 1
    X
    class Solution:
        # @param num, a list of integer
        # @return a list of integer
        def nextPermutation(self, num):
            for i in range(len(num)-2, -1, -1):
                for j in range(len(num)-1, i, -1):
                    if num[i] < num[j]:
                        num[i], num[j] = num[j], num[i]
                        return num[:i+1]+num[:i:-1]
                
            return num[::-1]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.