200328题(914.卡牌分组(最大公约数+哈希))

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
	bool hasGroupsSizeX(vector<int>& deck) {
		map<int, int> hash_map;//number-count
		for (auto x : deck)
			hash_map[x]++;
		int max = hash_map.begin()->second;//max为要找的最大公约数
		for (auto i = hash_map.begin(); i != hash_map.end(); i++)
		{
			max = gcd(max, i->second);
		}
		return max >= 2;
	}
	int gcd(int x, int y) {//找x、y的最大公约数
		return !x ? y : gcd(y % x, x);
	}


};

发布了241 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ShenHang_/article/details/105138003