I was solving the Rotate Array leetcode problem:
Given an array, rotate the array to the right by k steps, where k is non-negative.
I tested the following code on my computer (and it seems to do the required job):
nums = [1,2,3,4,5,6,7]
k = 3
nums = nums[-k:] + nums[:len(nums)-k]
print(nums)
>> [5,6,7,1,2,3,4]
Therefore, I tried the following solution:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
nums = nums[-k:] + nums[:len(nums)-k]
However, getting the following test case wrong:
Actually, this test case was tested successfully when I ran without defining any function (which I provided above). Yet, really, it didn't work when I defined a special function to rotate
. Then, I concluded that I might be doing something wrong when defining the function.