22. Rotate Array
MediumArrays~15 min
Rotate an array to the right by k steps. Elements pushed off the end wrap around to the front.
k is non-negative and may be larger than the array length.
Examples
Example 1
- Input:
- nums = [1, 2, 3, 4, 5], k = 2
- Output:
- [4, 5, 1, 2, 3]
Example 2
- Input:
- nums = [1, 2], k = 5
- Output:
- [2, 1]
- Why:
- Rotating by 5 is the same as rotating by 1.
Constraints
0 <= nums.length <= 10000 <= k <= 100000Return a new array.