Python got the different result in OJ and my own machine


  • 0
    H

    I try to solve it with python in a simple way:

    class Solution:
    # @param nums, a list of integer
    # @param k, num of steps
    # @return nothing, please modify the nums list in-place.
    def rotate(self, nums, k):
        n = len(nums)
        return nums[n-k:n]+nums[:n-k]
    

    However the solution I got is different from online. For example, [1,2], 1
    Where I got is [2,1], but the judger got [1, 2]


  • 0
    H

    @return nothing, please modify the nums list in-place.


  • 0
    U

    I got the same problem:

    class Solution:
    def rotate(self, nums, k):
    n = len(nums)
    m = []
    for i in range(n):
    m.append(nums[(i +n - k) % n])
    nums = m
    nums = []
    for i in range(n):
    nums.append(m[i])

    [1,2], 1 Where I got is [2,1], but the judger got [1, 2]


  • 0
    T

    How did you fix that? I got the same problem.
    Input: [1,2], 1
    Output: [1,2]
    Expected: [2,1]


Log in to reply
 

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