Leetcode 914. X of a Kind in a Deck of Cards

电玩女神 2022-09-09 00:12 144阅读 0赞

文章作者:Tyan
博客:noahsnail.com | CSDN | 简书

1. Description

X of a Kind in a Deck of Cards

2. Solution

**解析:**Version 1,统计元素个数,遍历所有可能的分割数量,下限为2,上限为最少的元素个数,如果满足条件,返回True

  • Version 1

    class Solution:

    1. def hasGroupsSizeX(self, deck: List[int]) -> bool:
    2. stat = collections.Counter(deck)
    3. for j in range(2, min(stat.values()) + 1):
    4. flag = True
    5. for val in stat.values():
    6. if val % j != 0:
    7. flag = False
    8. break
    9. if flag:
    10. return True
    11. return False

Reference

  1. https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读