leetcode 卡牌分组

https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/

class Solution {
public:
    int gcd(int x,int y) {return x?gcd(y%x,x):y;}
    bool hasGroupsSizeX(vector<int>& deck){
        int vis[10005]{0},ans;
        for(int i=0;i<deck.size();++i){ //记录个数
            vis[deck[i]]++; 
        }
        ans=vis[deck[0]];
        for(int i=0;i<deck.size();++i)  ans=gcd(ans,vis[deck[i]]); //如果互质则错,ans返回1
        return ans>=2;
    }
};
posted @ 2020-03-27 22:03  aaaaassss  阅读(119)  评论(0编辑  收藏  举报