Skip to content

Rotate Array

MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list nums, rotate it to the right by k steps in place: each step moves the last element to the front. k may be larger than the length. As a follow-up, use O(1) extra space.

nums = [1, 2, 3, 4, 5, 6, 7], k = 3   →  nums becomes [5, 6, 7, 1, 2, 3, 4]
nums = [-1, -100, 3, 99],    k = 2   →  nums becomes [3, 99, -1, -100]

The function returns nothing; the tests inspect nums after the call.

Constraints

  • 1 <= nums.length <= 10^5
  • -2^31 <= nums[i] <= 2^31 - 1
  • 0 <= k <= 10^5