难度:easy
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.
Hint:
Could you do it in-place with O(1) extra space?
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
思路:1.要求用三种方法写出,必须是原地算法(in-place),对原地算法。不是非常确切原地算法的概念,查了很多资料也没有找到完整的答案。应该是只能直接修改原数组,不能返回新的数组,不能用“return”,"x+=3"为in-place operator, 还可以用“-,*,%”等。
2. k=k%n, k<n时,k不变,k>=n时,k为(k-n)
方法1:用切割list的方法。
方法2: 把原list复制到一个新的list上面,然后用新list对原list重新赋值。这个是对list进行原地赋值。