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 ...
随机推荐
- 浅谈Cocos2d-js cc.director
在cocos2d-x里面,游戏的任何时间,只有一个场景对象实例处于运行状态,该对象可以作为当前游戏内容的整体包对象. 环境设定 进入游戏之前,导演会设置游戏的运行环境: 设置游戏视图,包含视图的投射, ...
- hibernate 的API使用
1.Query对象:不需要写sql语句,但需要hql语句,和sql很类似 (1)sql和hql区别:sql操作表和表字段,hql操作实体和实体属性 (2)使用: 2.Criteria对象:不需要写语句 ...
- dedecms 模版里格式化时间标签
<!--带时分秒--> [field:pubdate function="GetDateTimeMK(@me)"/] <!--只有日期--> [field: ...
- MVC实战之排球计分(五)—— Controller的设计与实现
控制器 控制器接受用户的输入并调用模型和视图去完成用户的需求.所以当单击Web页面中的超链接和发送HTML表单时, 控制器本身不输出任何东西和做任何处理.它只是接收请求并决定调用哪个模型构件去处理请求 ...
- Yii框架实现restful 接口调用,增删改查
创建模块modules; 在main.php中配置文件:(1) (2)控制器层: namespace frontend\modules\v1\controllers;use frontend\modu ...
- 加密、签名和SSL握手机制细节
openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.1 背景知识 对称加密 :加密解密使用同一密钥,加解密速度快.随 ...
- QComboBox样式
https://blog.csdn.net/lys211/article/details/43956979https://blog.csdn.net/x_nazgul/article/details/ ...
- ID基本操作(复制页面)(移动页面)(调整跨页页数)(版面调整)5.16
1.在页面面板中选择要复制的页面.拖动到新建页面图标上就可以新建页面. 2.在页面面板中选择要复制的页面.点击右上角的下箭头选择直接复制跨页. 3.在页面面板中选择要移动的页面.用鼠标拖到要移动的地方 ...
- C# 用 WebClient 的 Post 方法向 WebServer 传输数据
帮朋友做一个通过Web简单传输数据的例子,百度了一下抄了段代码,完成,效果如下: 其中textBox1里面是客户端需要传输过去的数据,textBox2里面是接收到的返回数据. 代码如下: using ...
- springmvc文件上传示例
首先要导包,用的的包是: commons-fileupload-*.*.*.jar commons-io-*.*.jar *号代表版本号 这里给大家分享一下下载链接:https://files.cnb ...