[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 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
Xcards. - 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 <= 100000 <= deck[i] < 10000
思路:
直接暴力,首先,x一定能整除deck的长度n。而且x也一定能整除group里每一个元素个数。
bool hasGroupsSizeX(vector<int>& deck)
{
int n = deck.size();
if(n <= )return false;
map<int,int>mp;
for(int i = ; i < n; i++){mp[deck[i]]++;} for(int x = ; x <= n; x++)
{
if( n % x != )continue;
for(auto it = mp.begin(); ;)
{
if(it->second % x != ) { break;}
it ++;
if(it == mp.end()){return true;}
}
}
return false;
}
[leetcode-914-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 (easy)
原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...
- 【Leetcode_easy】914. X of a Kind in a Deck of Cards
problem 914. X of a Kind in a Deck of Cards 题意:每个数字对应的数目可以均分为多组含有K个相同数目该数字的数组. 思路:使用 map 结构记录数组中每个元素 ...
- 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ...
- 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 ...
- LeetCode.914-一副牌中的X(X of a Kind in a Deck of Cards)
这是悦乐书的第352次更新,第377篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第214题(顺位题号是914).在一副牌中,每张牌上都写有一个整数. 当且仅当您可以选择 ...
- 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 choos ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LEETCODE】55、数组分类,适中级别,题目:79、611、950
第950题,这题我是真的没想到居然会说使用队列去做,大神的答案,拿过来瞻仰一下 package y2019.Algorithm.array; import java.util.HashMap; imp ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- maven多模块部署(转载)
[Maven]使用Maven构建多模块项目 Maven多模块项目 Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理.尤其是一些开源框架,也是采用多模块的 ...
- mysql面试常见题目
第一题 某班学生和考试成绩信息如下表Student所示: Student表 ID SName Mark 1 Jack 90 2 Marry 96 3 Rose 88 4 Bob 86 5 John 8 ...
- 关于osi的7层与tcp的4层网络协议的理解
osi 七层模型 应用层 提供接口 表示层 机器语言的二进制转换 对话层 决定是否传输 传输层 确定可不可靠 排差错 控流 网络层 提供逻辑地址 选路 数据链路层 mac 错误检测 物理层 设备间的比 ...
- JavaWeb开发使用jsp还是html做前端页面
一.概述 刚开始学习Javaweb开发的小伙伴都有一个疑惑:用jsp开发前端还是用HTML开发前端呢? 这个疑惑的来源主要是:刚接触完前端但又不深入学习js,接着学习jsp,发现老师们都一直用着jsp ...
- sklearn的train_test_split,果然很好用啊!
sklearn的train_test_split train_test_split函数用于将矩阵随机划分为训练子集和测试子集,并返回划分好的训练集测试集样本和训练集测试集标签. 格式: X_tra ...
- 检测com端口代码实现
1:scan HRESULT CDevHound::Scan(const vector<CString> &guiInfo, vector<DEV_INFO> & ...
- 双端队列 ADT接口 数组实现
Deque ADT接口 DEQUEUE.h: #include <stdlib.h> #include "Item.h" void DEQUEUEinit(int); ...
- Angular 7 版本
Angular 7 版本 这是跨整个平台的主要版本,更新包括核心框架,Angular Material和CLI. 如何更新到v7 可以访问update.angular.io以获取有关更新应用程序的详细 ...
- 20155305乔磊2016-2017-2《Java程序设计》第十周学习总结
20155305乔磊2016-2017-2<Java程序设计>第十周学习总结 教材学习内容总结 Java的网络编程 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. ...
- [2016北京集训测试赛5]azelso-[概率/期望dp]
Description Solution 感谢大佬的博客https://www.cnblogs.com/ywwyww/p/8511141.html 定义dp[i]为[p[i],p[i+1])的期望经过 ...