1. In a deck of cards, each card has an integer written on it.
  2.  
  3. 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:
  4.  
  5. Each group has exactly X cards.
  6. All the cards in each group have the same integer.
  7.  
  8. Example 1:
  9.  
  10. Input: [1,2,3,4,4,3,2,1]
  11. Output: true
  12. Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
  13. Example 2:
  14.  
  15. Input: [1,1,1,2,2,2,3,3]
  16. Output: false
  17. Explanation: No possible partition.
  18. Example 3:
  19.  
  20. Input: [1]
  21. Output: false
  22. Explanation: No possible partition.
  23. Example 4:
  24.  
  25. Input: [1,1]
  26. Output: true
  27. Explanation: Possible partition [1,1]
  28. Example 5:
  29.  
  30. Input: [1,1,2,2,2,2]
  31. Output: true
  32. Explanation: Possible partition [1,1],[2,2],[2,2]
  33.  
  34. Note:
  35.  
  36. 1 <= deck.length <= 10000
  37. 0 <= deck[i] < 10000

这道题仔细分析之后会得到X 一定得能被deck.length整除,也一定得被每个元素出现的次数整除,才能return true

  1. class Solution {
  2. public boolean hasGroupsSizeX(int[] deck) {
  3. if(deck == null || deck.length < 2){
  4. return false;
  5. }
  6. int min = Integer.MAX_VALUE;
  7. Map<Integer, Integer> map = new HashMap<>();
  8. for(int num : deck){
  9. map.put(num, map.getOrDefault(num,0)+1);
  10. }
  11. for(int x = 2 ; x<=deck.length; x++){
  12. if(deck.length % x != 0){
  13. continue;
  14. }
  15. int count = 0;
  16. for(int key : map.keySet()){
  17. count++;
  18. if(map.get(key) % x != 0){
  19. break;
  20. }
  21. if(count == map.size()){
  22. return true;
  23. }
  24. }
  25.  
  26. }
  27. return false;
  28. }
  29. }

LeetCode - 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 解题报告(Python & C++)

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

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

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

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

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

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

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

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

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

随机推荐

  1. A4988和CNC SHIELD使用方法 步进电机

    接线视频 点这看视频 来源 https://www.basemu.com/a4988_pinout_and_how_to_use.html 注意要点 A4988既要12V外部供电,也要5V逻辑供电 我 ...

  2. 【HNOI 2018】排列

    Problem Description 给定 \(n\) 个整数 \(a_1, a_2, \ldots , a_n(0 \le a_i \le n)\),以及 \(n\) 个整数 \(w_1, w_2 ...

  3. _luckdraw

    该表可以控制进行抽奖.10连抽: `comment` 备注 `itemId` 物品ID `chance`几率 `itemCount` 数量

  4. Java原生API访问MongoDB

    1.pom.xml <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java ...

  5. SR-IOV虚拟机的MTU与物理网卡的MTU

    在进行SR-IOV虚拟机MTU方面的测试时,出现如下情况: 1)物理网卡PF的MTU值是4000: root@compute-1:~# ip l|more1: lo: <LOOPBACK,UP, ...

  6. Matlab:五点差分方法求解椭圆方程非导数边值问题

    差分格式脚本文件: tic; clear clc M=32;%x的步数 N=16;%y的步数 h1=1/M;%x的步长 h2=1/N;%y的步长 x=0:h1:1; y=0:h2:1; u=zeros ...

  7. Oracle DB 使用RMAN恢复目录

    • 对恢复目录和RMAN 资料档案库控制文件的使用进行比较• 创建和配置恢复目录• 在恢复目录中注册数据库• 同步恢复目录• 使用RMAN 存储脚本• 备份恢复目录• 创建虚拟专用目录 RMAN 资料 ...

  8. 微信支付---公众号支付和H5支付区别

    微信支付分为如下几种:(来源https://pay.weixin.qq.com/wiki/doc/api/index.html) 本文主要讲解公众号支付和H5支付,两者均属于线上支付比较常用的方式: ...

  9. html5(二)

    *{ margin:0px; padding:0px;} h1{ font:bold 20px verdana,sans-serif;} h1{ font:bold 14px verdana,sans ...

  10. .net core部署到Ubuntu碰到的问题

    数据库连接的时候,会报错“MySql.Data.MySqlClient.MySqlException:“The host localhost does not support SSL connecti ...