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?
Related problem: Reverse Words in a String II
解法1:一个最简单的想法就是每次将数组后移一步,一共循环k次。满足in-place,但是时间复杂度为O(k*n)。
class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); if(n < 2 || k < 1) return; for(int i = 1; i <= k; i++) { int tmp = nums[n - 1]; for(int j = n - 1; j > 0; j--) nums[j] = nums[j - 1]; nums[0] = tmp; } } };
解法2:[1,2,3,4,5,6,7,8],k=3,旋转后为[6,7,8,1,2,3,4,5],对比前后每个元素的下标发现规律index2=(index1+k)%n,n为数组长度。时间复杂度为O(n),空间复杂度O(n)。
class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); if(n < 2 || k < 1) return; vector<int> res(n, 0); for(int i = 0; i < n; i++) res[(i + k) % n] = nums[i]; copy(res.begin(), res.end(), nums.begin()); } };
解法3:[1,2,3,4,5,6,7,8],k=3,旋转后为[6,7,8,1,2,3,4,5],可以分2步进行:(1)将整个数组旋转(k=n),得到[8,7,6,5,4,3,2,1];(2)以k为分界点,分别旋转第一次旋转后数组的前后两部分,即[8,7,6]和[5,4,3,2,1],得到[6,7,8]和[1,2,3,4,5],即是需要的结果。时间复杂度O(n),空间复杂度O(1)。
注意:需要将k归化到小于n的情形下。因为k=n时相当于不旋转,因此可以重新取k=k%n。
class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); k %= n; reverse(nums.begin(), nums.end()); reverse(nums.begin(), nums.begin() + k); reverse(nums.begin() + k, nums.end()); } };
或者以n - k为界,分别对数组的左右两边执行一次逆置;然后对整个数组执行逆置。
class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); k %= n; reverse(nums.begin(), nums.begin() + n - k); reverse(nums.begin() + n - k, nums.end()); reverse(nums.begin(), nums.end()); } };