天天看点

【2019秋冬】【LeetCode】189 旋转数组

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int s = nums.size();
        k %= s;
        if(k==0 || s < 2) return ;
        int time = gcd(s,k);
        for(int i=0; i<time; i++){
            int temp = nums[i];
            int j = i + k;
            while(j%s!=i){
                int temp1;
                temp1 = nums[j%s];
                nums[j%s] = temp;
                temp = temp1;
                j+=k;
                
            }
            nums[i] = temp;
        }
        
    }
    int gcd(int a,int b){
        return b==0 ? a : gcd(b,a%b);
    }
    
};
           

思路都对,写代码写了一上午

太菜了!!!

复习最大公约数最小公倍数写法