Rotate Array
Given an integer array nums and a non-negative integer k, rotate the array to the right by k positions, in place. After one step to the right, every element moves one index higher and the last element wraps around to index 0.
For example, rotating [1, 2, 3, 4] by 1 gives [4, 1, 2, 3].
Modify nums itself and return nothing. k may be larger than the length of the array. Try to use only O(1) extra space. In Python, assign through the slice (nums[:] = ...) or swap elements: rebinding the name nums does not change the caller's list.
Constraints
1 <= nums.length <= 10^5-2^31 <= nums[i] <= 2^31 - 10 <= k <= 10^9
Examples
Example 1
Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
The last three values 5, 6, 7 move to the front, giving [5, 6, 7, 1, 2, 3, 4].
Example 2
Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100]
Rotating by 2 moves 3 and 99 to the front.
Example 3
Input: nums = [1,2], k = 5 Output: [2,1]
5 steps on a 2-element array is the same as 1 step (5 % 2), which swaps the two values.
JavaScript you'll use: Arrays: create, read, update Destructuring, spread and rest
/**
* @param {number[]} nums
* @param {number} k
* @return {void}
*/
function rotate(nums, k) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.