解题思路:最大公约数法,最后判断是否大于等于2
原来c++有自带的__gcd函数。。。。
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
if (deck.size()<2) return false;
map<int,int> mymap;
for (auto w:deck) {
mymap[w]++;
}
if(mymap.size()==1) return true;
auto it = mymap.begin();
int prev = it->second;
if(prev<2) return false;
it++;
for (; it != mymap.end(); ++it) {
if (measure(it->second,prev)==1) return false;
prev = it->second;
}
return true;
}
int measure(int x, int y) {
int z = y;
while(x%y!=0) {
z = x%y;
x = y;
y = z;
}
return z;
}
};