【leetcode 最大公约数 C++】914. X of a Kind in a Deck of Cards

914. X of a Kind in a Deck of Cards

在这里插入图片描述

class Solution {
    
    
public:
    int GCD(int a, int b) {
    
    
        if(a < b) return GCD(b, a);
        while(b) {
    
    
            int c = a % b;
            a = b;
            b = c;
        }
        return a;
    }
    bool hasGroupsSizeX(vector<int>& deck) {
    
    
        map<int, int> M;
        for(int ii = 0; ii < deck.size(); ii++) {
    
    
            if(M.find(deck[ii]) == M.end()) M[deck[ii]] = 1;
            else M[deck[ii]]++;
        }
        int gcd = 0;
        for(map<int, int>::iterator it = M.begin(); it != M.end(); it++) {
    
    
            if(!gcd) gcd = it->second;
            else gcd = GCD(gcd, it->second);
            if(gcd == 1) return false;
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/113861010
今日推荐