class Solution { public: void myReverse(vector<int> &nums,int start, int end) { while(start < end) { swap(nums[start++], nums[end--]); } } void rotate(vector<int>& nums, int k) { if(nums.size()<2) return; k = k % nums.size(); myReverse(nums, 0, nums.size() - k - 1); myReverse(nums, nums.size()-k, nums.size() - 1); myReverse(nums, 0 , nums.size() - 1); } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter