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. 1 <= deck.length <= 10000
    2. 0 <= 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]的更多相关文章

  1. [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 ...

  2. [leetcode]914. X of a Kind in a Deck of Cards (easy)

    原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...

  3. 【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 结构记录数组中每个元素 ...

  4. 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ...

  5. 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 ...

  6. LeetCode.914-一副牌中的X(X of a Kind in a Deck of Cards)

    这是悦乐书的第352次更新,第377篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第214题(顺位题号是914).在一副牌中,每张牌上都写有一个整数. 当且仅当您可以选择 ...

  7. 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 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. 【LEETCODE】55、数组分类,适中级别,题目:79、611、950

    第950题,这题我是真的没想到居然会说使用队列去做,大神的答案,拿过来瞻仰一下 package y2019.Algorithm.array; import java.util.HashMap; imp ...

  10. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. java实现zabbix接口开发

    API:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/user/login 如果你使用jar包开发的话,会出现*** ...

  2. 有关集合的foreach循环里的add/remove

    转自:Hollis(微信号:hollischuang) 在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 1 .foreach循环 ...

  3. 1004. Counting Leaves(30)—PAT 甲级

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  4. MySQL学习【第十三篇日志管理】

    一.MySQL日志类型 日志文件 选项  文件名/表名称   程序 错误日志 --log-error   host_name.err N/A 常规日志 --general_log host_name. ...

  5. Linux基础练习题之(四)

    Linux基础练习题 请详细总结vim编辑器的使用并完成以下练习题 1.复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的 ...

  6. (1)linux和oracle---环境搭建

    对linux和oracle一直是敬而远之,稍微有些了解.无奈由于工作需要这次要硬着头皮上了!@#!@@#$%^^ 对于重windows用户的我来说,简直是万种折磨. 算是做个记录吧,一定要坚持下去. ...

  7. 基于 HTML5 Canvas 的拓扑组件开发

    在现在前端圈大行其道的 React 和 Vue 中,可复用的组件可能是他们大受欢迎的原因之一, 在 HT 的产品中也有组件的概念,不过在 HT 中组件的开发是依托于 HTML5 Canvas 的技术去 ...

  8. 1、win10下的Docker+Redis 的下载及简单使用

    一.下载Docker: 因为始终注册docker账号不成功,所以在这里点击下载. 选中docker-for-windows/ 选中beta/ 下载这个.msi文件 二.安装 1.安装.msi文件,桌面 ...

  9. 基于OpenCV的微信跳一跳外挂

    摘要:微信跳一跳是时下热门的微信小游戏,基本原理是根据按压屏幕的时间控制棋子跳过的距离,使其跳到下一个方块上:现利用Android adb工具,PC端获取实时截图,使用OpenCV库分析图片计算距离, ...

  10. python多进程详解和协程

    1.由于python多线程适合于多IO操作,但不适合于cpu计算型工作,这时候可以通过多进程实现.python多进程简单实用 # 多进程,可以cpu保持一致,python多线程适合多io.对于高cpu ...