Rotate Array
Given an integer array nums, rotate the array to the right by k steps.
Implement rotate(nums, k) so that it modifies nums in place and returns nothing; the tests print nums after the call as space-separated integers.
k may be larger than the length of nums.
Example
1,2,3,4,5,6,7 3
5 6 7 1 2 3 4
numsis the array with elements, and is how many positions every element shifts to the right.- One step right moves each element up one index, and the one falling off the end wraps to index . After steps, the element at index lands at index .
- Work it through: at index goes to index ; at index goes to index ; at index goes to ; at index goes to .
- Reading the new positions to gives
5 6 7 1 2 3 4: the last three values now lead, and the first four keep their relative order behind them. - Notice that rotating by returns every element to where it started, so gives the same result as , and leaves the array unchanged.
Constraints
- 1 <= len(nums) <= 10^5
- -2^31 <= nums[i] <= 2^31 - 1
- 0 <= k <= 10^5