原题地址:https://leetcode.com/problems/rotate-array/#/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]
.
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
[show hint]
Related problem: Reverse Words in a String II
这是一道简单题目,写两个解法。两个解法的时间复杂度均为o(n),第一种解法的空间复杂度为o(1),第二种解法的空间复杂度为o(n)。
解法一:
解法二:
可见,第一种时间上效率稍微好点,空间占用也比较少。第一种方法稍微好一些。