Leetcode914.X of a Kind in a Deck of Cards卡牌分组
给定一副牌,每张牌上都写着一个整数。
此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组:
- 每组都有 X 张牌。
- 组内所有的牌上都写着相同的整数。
仅当你可选的 X >= 2 时返回 true。
示例 1:
输入:[1,2,3,4,4,3,2,1] 输出:true 解释:可行的分组是 [1,1],[2,2],[3,3],[4,4]
示例 2:
输入:[1,1,1,2,2,2,3,3] 输出:false 解释:没有满足要求的分组。
示例 3:
输入:[1] 输出:false 解释:没有满足要求的分组。
示例 4:
输入:[1,1] 输出:true 解释:可行的分组是 [1,1]
示例 5:
输入:[1,1,2,2,2,2] 输出:true 解释:可行的分组是 [1,1],[2,2],[2,2]
提示:
- 1 <= deck.length <= 10000
- 0 <= deck[i] < 10000
求最大公因数
int gcd(int x, int y)
{
int MAX = max(x, y);
int MIN = min(x, y);
return MAX % MIN == 0? MIN : gcd(MIN, MAX % MIN);
}
class Solution {
public:
bool hasGroupsSizeX(vector<int>& deck) {
map<int, int> save;
int len = deck.size();
if(len == 1)
return false;
for(int i = 0; i < len; i++)
{
save[deck[i]]++;
}
vector<int> v;
for(map<int, int> :: iterator itr = save.begin(); itr != save.end(); itr++)
{
v.push_back(itr ->second);
}
int res = v[0];
for(int i = 1; i < v.size(); i++)
{
res = gcd(res, v[i]);
}
if(res >= 2)
return true;
return false;
}
};
Leetcode914.X of a Kind in a Deck of Cards卡牌分组的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
- 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 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 ...
- [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 ...
随机推荐
- Last- Linux必学的60个命令
1.作用 last命令的作用是显示近期用户或终端的登录情况,它的使用权限是所有用户.通过last命令查看该程序的log,管理员可以获知谁曾经或企图连接系统. 2.格式 1ast[—n][-f file ...
- koa中同步与异步的写法
koa中间件洋葱圈模型,同步的写法 //node server.js //中间件机制 const Koa = require('koa') const app = new Koa() // app.u ...
- mysql兼容性语句
SHOW /*!50 GLOBAL */ STATUS SHOW /*!500 GLOBAL */ STATUS SHOW /*!5000 GLOBAL */ STATUS 都报错,必须是 SHOW ...
- springboot核心技术(三)-----web开发
web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运 ...
- Hibernate命名策略
hibernate的命名策略,可以减少对数据库标识符命名的维护,进一步减少这部份命名的重复性代码量,以提高维护. hibernate的命名方式,有两类,一类是显式命名,一类是隐式命名. 显式命名:在映 ...
- 第15章 RMAN备份
第15章 RMAN备份 oracle推荐的备份工具是rman(恢复管理器:recovery manager),用操作系统命令执行的备份被称为用户管理的备份.使用rman执行的备份被称为服务器管理备份. ...
- 解Bug之路-记一次中间件导致的慢SQL排查过程
解Bug之路-记一次中间件导致的慢SQL排查过程 前言 最近发现线上出现一个奇葩的问题,这问题让笔者定位了好长时间,期间排查问题的过程还是挺有意思的,正好博客也好久不更新了,就以此为素材写出了本篇文章 ...
- 设置eclipse自动补全
点击" Window>Preferences"; 选择"Java>Editor>Content Assist",在右侧的"Auto- ...
- phonegap geolocation android 问题
很纠结的 phonegap 使用定位的时候 android 获取地址异常的慢,为什么呢? 经过分析 如果android 只开启gprs 上网功能 可以立即获取到经纬度 如果只开启wifi 根本就获取不 ...
- NOSQL数据库之 REDIS
NOSQL数据库之 REDIS 一.NOSQL 1.简介 NoSQL ,(Not Only SQL),泛指非关系型数据库. 特点: NoSQL 通常是以key-value形式存储, 不支持SQL语句, ...