Rotate Array
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Rotate Array
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1
Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Example 2
Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100]
Show hint
Reverse the entire array, then reverse the first k elements, then reverse the remaining elements. Or use extra space to store rotated elements.
Sign in to submit, save progress and mark problems as solved. You can still run your code.
Loading editor...
nums =
[1,2,3,4,5,6,7]
k =
3
Expected
[5,6,7,1,2,3,4]