LeetCode高频面试60天打卡日记Day27

Day27(卡牌分组–最大公约数)

在这里插入图片描述
idea:

求数组中各个相同元素个数的最大公约数,如果最大公约数>=2则返回true 否则返回false

class Solution {
    
    
    public boolean hasGroupsSizeX(int[] deck) {
    
    
        int[] counter = new int[10000];
        for(int i=0;i<deck.length;i++){
    
    
            counter[deck[i]] ++;
        }
        int x =0;
        for(int count:counter){
    
    
            if(count>0){
    
    
                //计算最大公约数
                x = gcd(x,count);
                if(x==1){
    
    
                    return false;
                }
            }
        }
        return x>=2;
    }
    
    private int gcd(int a,int b){
    
    
        if(a==0) return b;
        if(b==0) return a;
        int max = a>b? a:b;
        int min = a<b? a:b;
        if(max%min==0){
    
    
            return min;
        }
        return gcd(max%min,min);
    }
    
}

猜你喜欢

转载自blog.csdn.net/YoungNUAA/article/details/105171292