914 LeetCode 卡牌分组

题目描述:
在这里插入图片描述

思路:
首先使用哈希表记录每个数出现的次数
然后判断最小的次数是否大于等于2
最后判断能否在最小的次数范围内找到公约数

代码如下:

class Solution {
public:
    bool hasGroupsSizeX(vector<int>& deck) {
        map<int,int>cnt;
        int num=deck.size();
        for(int i=0;i<deck.size();i++){
            cnt[deck[i]]++;
        }
        for(int i=0;i<deck.size();i++){
            num=min(num,cnt[deck[i]]);
        }
        if(num<2)  return false;
        for(int j=2;j<=num;j++){
            int temp=0;
            for(int i=0;i<deck.size();i++){
               if(cnt[deck[i]]%j!=0)
               temp=1;
            }
            if(temp==0)
            return true;
        }
        return false;
    }
};
发布了158 篇原创文章 · 获赞 0 · 访问量 1624

猜你喜欢

转载自blog.csdn.net/peachzy/article/details/104532707
今日推荐