Rotate Array
Last updated
Was this helpful?
Last updated
Was this helpful?
Rotate an array of n elements to the right by k steps.
For example, withn= 7 andk= 3, the array[1,2,3,4,5,6,7]
is rotated to[5,6,7,1,2,3,4]
.
Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Hint:
Could you do it in-place with O(1) extra space?
Related problem:
三步翻转法
reverse the first n - k elements
reverse the rest of them
reverse the entire array
O(n) in time, O(1) in space
剩下解法:
For reversing the same list use:
To assign reversed list into some other list use: