[Leetcode-算法]914.卡牌分组

题目链接:戳这里
题目描述:
[Leetcode-算法]914.卡牌分组_第1张图片
解题思路:
用HashMap来存以数组中数为key,出现次数为value的键值对,如果有value为奇数,直接返回false;否则看value的值是不是有公约数。
此题我代码效率不高,待优化。
java代码:

class Solution {
        public boolean hasGroupsSizeX(int[] deck) {
            int len=deck.length;
            Map tmp=new HashMap();
            for(int i=2;i<=len;i++){
                if(len%i!=0){
                    continue;
                }
                if(i>2&&i%2==0){
                    continue;
                }
                for(int j=0;j it = tmp.values().iterator(); it.hasNext(); ) {
                    Integer n = it.next();
                    if(n%i==0){
                        flag++;
                        continue;
                    }else {
                        break;
                    }
                }
                if(flag==tmp.size()){
                    return true;
                }else {
                    tmp.clear();
                    continue;
                }
            }
            return false;
        }
    }

你可能感兴趣的:(leetcode,java,hashmap)