408算法题leetcode--第八天
189. 轮转数组
- 189. 轮转数组
- 思路:翻转数组
- 时间:O(n);空间:O(1)
class Solution {
public:void reverse(vector<int>& nums, int start, int end){for(int i = start, j = end; i < j; i++, j--){swap(nums[i], nums[j]);}}void rotate(vector<int>& nums, int k) {// reverse三次k %= nums.size(); // 注意特例reverse(nums, 0, nums.size() - 1);reverse(nums, 0, k - 1);reverse(nums, k, nums.size() - 1);}
};