给定一副牌,每张牌上都写着一个整数。

此时,你需要选定一个数字 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. 1 <= deck.length <= 10000
  2. 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卡牌分组的更多相关文章

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

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

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

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

  5. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

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

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

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

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

随机推荐

  1. 关于Canvas的坐标系

    注意Canvas的坐标系应该是这样子的: 看下面的例子: 最后的显示效果是:

  2. ROS urdf和xacro文件详解

    视觉标签:visual <visual> <origin xyz="0.0 0.0 0.0" /> <geometry> <box siz ...

  3. 一条sql获取每个类别最新的一条记录

    1.初始化数据 create table Products ( id ,), name ), categroy int, addtime datetime , ) insert into Produc ...

  4. c语言一个例子对比php学习随记

    今天开始学写一些简单的C例子,发现很多和PHP不一样的地方,做个笔记. #include <stdio.h> int main(void) { int a,b; printf(" ...

  5. 使用内部Servlet转发JSP后页面的JS,CSS等资源引入问题的解决

    转载自:https://blog.csdn.net/weixin_44353336/article/details/90677792 问题原因 我们在进行web项目开发过程中,经常会在web页面引入一 ...

  6. LeetCode简单算法之分割平衡字符串 #1221

    在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...

  7. PHP学习1.5-预定义超全局数组变量

    1.PHP 预定义的超全局变量数组 特性: a.特殊的数组,操作方式没有区别 b.不用声明,php脚本中默认存在,因为在php中不用定义,所以在自定义变量是应避免和预定的全局变量同名 c.在全局范围内 ...

  8. js的几种继承方式

    1.原型链方式 function Super(){ this.val = 1; this.arr = [1]; } function Sub(){ // ... } Sub.prototype = n ...

  9. 读书笔记--Apache.Tomcat.6高级编程 目录

    1.Apache Tomcat 2.web的应用:Servlet.JSP及其他 3.Tomcat安装 4.Tomcat架构 5.Tomcat基本配置 6.高级Tomcat特性 7.Web应用程序配置 ...

  10. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...