Python: local test passed but online judge failed. Bug?


  • 0
    J
    class Solution(object):
        def rotate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            nums = nums[-k:] + nums[:-k]
    
    x = Solution()
    x.rotate([1,2], 1)
    

    Local output: [2, 1]
    [Finished in 0.054s]

    Online judge: Output:
    [1,2]


  • 0
    P

    I'm having the same problem! It gives me the wrong answer for that test case. Code works on local machine
    This is my code -

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

    Did you get it resolved by any chance?


  • 0
    J

    I have the same problem too. Here is my code:

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

  • 0
    D

    List concatenation will create a new "nums", it does not modify list in-place.


Log in to reply
 

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