Leetcode Algorithms -- Rotate Array
Rotate Array (Easy)
Description
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Analysis
这题类似编程之美上一题的思路。相当于翻转字符串。
以n - k为界,分别对数组的左右两边执行一次逆置;然后对整个数组执行逆置
My Solution
1 | //C++ |