914. 卡牌分组
思路:哈希表+最大公约数。先遍历一遍数组,用哈希表统计出不同数出现的次数,再求这些数的最大公约数x。
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(auto t:deck){
mp[t]++;
}
int x=0;
for(auto [a,b]:mp){
x=gcd(x,b);
}
return x>=2;
}
};