Why is the local runtime result of this code is different from OJ


  • 0
    T
    class Solution:
        def rotate(self, nums, k):
            k %= len(nums)
            nums = nums[::-1][:k][::-1] + nums[::-1][k:][::-1]
            return nums
    

    Submission Result: Wrong Answer

    Input:	[1,2], 1
    Output:	[1,2]
    Expected:	[2,1]

  • 0
    F

    you should just modify nums itself.


  • 0
    T

    class Solution:

    def rotate(self, nums, k):
    
        k %= len(nums)
    
        nums = nums[::-1][:k][::-1] + nums[::-1][k:][::-1]
    

    remove the statement "return nums", the code still get wrong answer


  • 0
    R

    def rotate(self, nums, k):
    k %= len(nums)
    nums = nums[-k:] + nums[:len(nums)-k]

    Input: [1,2], 1
    Output: [1,2]
    Expected: [2,1]
    why I also get the wrong answer


  • 0
    T

    it should be:

    k %= len(nums)
    nums[:] = nums[::-1][:k][::-1] + nums[::-1][k:][::-1]
    

Log in to reply
 

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