Simple Python beats 96.75%


  • 0
    M

    '''

    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        # find the rightmost digit with ascending order
        i = len(nums)-1
        while i > 0 and nums[i-1] >= nums[i]:
            i -= 1
        # find the right most digit greater than nums[i-1]
        if i > 0:
            j = len(nums)-1
            while nums[j] <= nums[i-1]:
                j -= 1
            nums[i-1], nums[j] = nums[j], nums[i-1]
        # reverse the list from ith to end
        j = len(nums) - 1
        while i < j:
            nums[i], nums[j] = nums[j], nums[i]
            i += 1
            j -= 1 
        return
    

    '''


Log in to reply
 

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