题目链接: 这这这
思路:先把每个元素的个数计数,保存起来。再找他们的最大公约数gcd,如果gcd>=2则可以划分,否则不能
class Solution {
public:
int GCD(int a,int b) { return b?GCD(b,a%b):a;}
bool hasGroupsSizeX(vector<int>& deck) {
unordered_map<int,int>mp;
for(const auto &ele:deck) mp[ele]++;
int nowGcd = 0;
for(const auto& it:mp){
nowGcd = GCD(it.second,nowGcd);
if(nowGcd<2) return false;
}
return true;
}
};