I am working on a leetcode question 189. Rotate Array.
Problem statement: Given an array, rotate the array to the right by k steps, where k is non-negative. Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Following is my code. My understanding is that the time complexity is O(k), but it ends up being very low. Could you please shine some light on this and educate me?
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
while k:
nums.insert(0,nums.pop())
k -= 1