Rotate Array
Given an array, rotate the array to the right by k _steps, where _k is non-negative.
Example 1:
Example 2:
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space?
Analysis
孪生兄弟题:Rotate String
3-step reverse 三步翻转法
经典的应用场景,左移或者右移都适用。需要注意的是把位移量k先对长度n取模,k = k % n;
extra auxiliary array 额外辅助数组
arr[(i + k) % nums.length] = nums[i];
Solution
3-step reverse - O(n) time, O(1) space
external aux array - O(n) time, O(n) space
Reference
Last updated