1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: void rotate(vector<int>& nums, int k) { k %= nums.size(); reverse(nums, 0, nums.size() - 1); reverse(nums, 0, k - 1); reverse(nums, k, nums.size() - 1); } private: void reverse(vector<int>& nums, int start, int end) { while (start < end) swap(nums[start++], nums[end--]); } };
|