Leetcode claiming incorrect answer when it's inarguably correct


I’m trying to complete the 189. Rotate Array problem on Leetcode.
This is the code I’ve written:

class Solution(object):
    def rotate(self, nums, k):
        end = len(nums) - 1
        carry = []
        new = [n for n in nums]
        for i in range(end-k+1, end+1):
        for i in range(1, k+1):
        for n in new:
        return carry

(The obsolete variables were to try and erase any possibility of something wacky happening with Leetcode’s testing system.)

I can run this code anywhere (I’ve tried VS Code and an online interpreter) and the result is always correct (for the first case, [5, 6, 7, 1, 2, 3, 4]).
However, when I try in Leetcode:

The stdout shows the correct answer literally the line before the return, and yet the array somehow magically changes through no tampering.

This is driving me up the wall… please help!!

(Problem description:

Given an array, rotate the array to the right by k steps, where k is non-negative.


Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:

Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]


1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105

Follow up:

Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
Could you do it in-place with O(1) extra space?


>Solution :

The Python template for this problem says:

class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        Do not return anything, modify nums in-place instead.

As the docstring mentioned, you’ll need to modify nums in-place. There is no need to print or return anything.

A simple fix to your existing code would be to use an assignment nums[:] = carry at the end.

Leave a Reply Cancel reply