LeetCode - X of a Kind in a Deck of Cards
In a deck of cards, each card has an integer written on it. Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where: Each group has exactly X cards.
All the cards in each group have the same integer. Example 1: Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
Example 2: Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.
Example 3: Input: [1]
Output: false
Explanation: No possible partition.
Example 4: Input: [1,1]
Output: true
Explanation: Possible partition [1,1]
Example 5: Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2] Note: 1 <= deck.length <= 10000
0 <= deck[i] < 10000
这道题仔细分析之后会得到X 一定得能被deck.length整除,也一定得被每个元素出现的次数整除,才能return true
class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        if(deck == null || deck.length < 2){
            return false;
        }
        int min = Integer.MAX_VALUE;
        Map<Integer, Integer> map = new HashMap<>();
        for(int num : deck){
            map.put(num, map.getOrDefault(num,0)+1);
        }
        for(int x = 2 ; x<=deck.length; x++){
            if(deck.length % x != 0){
                continue;
            }
            int count = 0;
            for(int key : map.keySet()){
                count++;
                if(map.get(key) % x != 0){
                    break;
                }
                if(count == map.size()){
                    return true;
                }
            }
        }
        return false;
    }
}
LeetCode - X of a Kind in a Deck of Cards的更多相关文章
- [LeetCode] 914. X of a Kind in a Deck of Cards 一副牌中的X
		In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ... 
- 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ... 
- [Swift]LeetCode914.一副牌中的X | X of a Kind in a Deck of Cards
		In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ... 
- 914. X of a Kind in a Deck of Cards
		In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ... 
- codeforces 744C Hongcow Buys a Deck of Cards
		C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ... 
- X of a Kind in a Deck of Cards LT914
		In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ... 
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
		Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ... 
- [leetcode-914-X of a Kind in a Deck of Cards]
		In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ... 
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards
		地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ... 
随机推荐
- galera+mycat高可用集群部署
			环境描述 10.30.162.29 client 环境描述 10.30.162.29 client 10.30.162.72 mysql1 10.30.162.73 mysql2 10.30.162 ... 
- js在线富文本插件的考虑
			使用之前需要考虑的点: 1需要插件,是否需要很多的插件,还是说简单的那些功能就行了. 2界面考虑,看你喜欢那个界面了. 3图片是否需要上传图片服务器. 4文档如果为英文是否会影响开发. 5支持浏览器类 ... 
- spoj1433 KPSUM
			题意:略: 首先知道10,20,......100,200,1000的前面的符号都是负号. 举具体例子:221时,计算过程为 000-009, 010-019, 020-029...... ... 
- js里面关于日期转换的问题
			我们拿到一个日期字符串:"2017-09-03",我们用new Date("2017-09-03")去转换成日期格式的时候,发现在火狐会报错,是因为火狐不支持这 ... 
- vue的组件之间传值方法
			父组件 <template> <div> 这是父组件 <children v-bind:parentToChild="toChild" v-on:sh ... 
- mysql查询今天、昨天、上周
			mysql查询今天.昨天.上周 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE ... 
- 牛客网多校第3场C-shuffle card 平衡树或stl(rope)
			链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ... 
- c函数 文件名通配符
			static bool IsMatched(CONST TCHAR* p, CONST TCHAR* q) { CONST TCHAR *r, *z, *x = _T(""); f ... 
- JS-使用indexof来统计字符出现次数
			<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ... 
- layer中每次用到都要查来查去的功能
			1.关闭当前弹出层 var index = parent.layer.getFrameIndex(window.name); setTimeout(function(){parent.layer.cl ... 
