A strange problem


  • 0
    L

    when input is [3,2,1],the result given by my computer is [1,2,3],but the submit result is [3,2,1],i don't know why ,could someone help me?

        i = len(nums)-1
        index = 0
        while i>0:
            if nums[i]>nums[i-1]:
                index = i
                break
            else:
                i -= 1
        if i == 0:
            nums = sorted(nums)
        else:
            mi = min(i for i in nums[index:] if i>nums[index-1])
            loc = nums[index:].index(mi)
            nums[index-1],nums[index+loc] = nums[index+loc],nums[index-1]
            nums[index:] = sorted(nums[index:])

  • 0
    R

    I had similar problems, and it was solved by replacing "nums = sorted(nums)" with "nums[:] = sorted(nums)". Check it out. :)


Log in to reply
 

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