189. 轮转数组

189. 轮转数组


题目链接:189. 轮转数组

代码如下:

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        k=k%nums.size();

        reverse(nums,0,nums.size()-1);//整体反转
        reverse(nums,0,k-1);//反转后的k个反转
        reverse(nums,k,nums.size()-1);//k个之后的翻转
        
    }

    void reverse(vector<int>& nums,int low,int high)
    {
        while(low<high)
        {
            swap(nums[low],nums[high]);
            low++,high--;
        }
    }
};

你可能感兴趣的:(leetcode,c++)