题目描述
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
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?
我的解法
解题思路
题目要求讲一个数组向右移动几位,最先想到的办法就是另外声明一个数组,然后将位移后的数组存到新数组里,最后再将新数组复制到旧数组里。
实现代码
1 | class Solution { |
Runtime: 16 ms, faster than 88.16% of C++ online submissions for Rotate Array.
Memory Usage: 9.6 MB, less than 50.79% of C++ online submissions for Rotate Array.
优化方法1
提示说能不能用O(1)的方法,想了一下没有想到条件要怎么设置。看了下讨论区,有一种环状替换的方法
实现代码
1 | class Solution { |
优化方法2
还看到一个用reverse的方法,只需要4行代码…
实现代码
1 | class Solution { |