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 ...
随机推荐
- Android开发 Camera2开发_1_拍照功能开发
介绍 google已经在Android5.1之后取消了对Camera1的更新,转而提供了功能更加强大的Camera2.虽然新版本依然可以使用Camera1但是,不管是各种机型适配还是拍照参数自定义都是 ...
- 使用 top instance 命令查看运行中 MaxCompute 作业
我们都知道,在 MaxCompute Console 里,可以使用下面的命令来列出运行完成的 instance 列表. show p|proc|processlist [from <yyyy-M ...
- JS的第七种语言类型--symbol
今天浏览网页的时候发现,JS中有七种语言类型.我的内心???百度一下哪里来的第七种!! 好吧跟着来回顾一下JS的前6种undefined null boolean string numver obje ...
- PAT甲级——A1017 Queueing at Bank
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...
- Leetcode458.Poor Pigs可怜的小猪
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...
- 字符串常用方法(转载--https://www.cnblogs.com/ABook/p/5527341.html)
一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...
- Centos Apache 多站点配置
首先明白APACHE配置文件位置 /etc/httpd/ 系统会自动加载 "/etc/httpd/conf.d" 目录下面的 "*.conf"文件 创建多个 & ...
- Functional Programming Contest - September'14
比赛链接 A题 -- Enter your code here. Read input from STDIN. Print output to STDOUT main = do x <- get ...
- zabbix自定义监控redis
zabbix监控redis脚本 #!/bin/bash #此脚本用来获取redis-cli info信息 redis_cli="/usr/local/redis/bin/redis-cli& ...
- Java中的String,StringBuffer和StringBuilder
在了解这个问题的时候查了不少资料,最有帮助的是这个博文:http://swiftlet.net/archives/1694,看了一段时间,咀嚼了一段时间,写一个经过自己消化的博文,希望能帮到大家. 首 ...