zoukankan      html  css  js  c++  java
  • 914. 卡牌分组

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

    此时,你需要选定一个数字 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

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards

    就是找每种牌出现的次数是否有大于等于2的公共质因数

    丢人解法

    class Solution:
        def hasGroupsSizeX(self, deck: List[int]) -> bool:
            n=len(deck)
            x=n
            if n==1:return False
            if n==2:return deck[0]==deck[1]
            dict=[]
            for i in set(deck):
                dict.append(deck.count(i))
                x=min(x,deck.count(i))
            if x==1:return False
            if x%2==0:
                flag=True
                for i in dict:
                    if i%2!=0:flag=False
                if flag:
                    return True
            if x%3==0:
                flag=True
                for i in dict:
                    if i%3!=0:flag=False
                if flag:
                    return True
            if x%5==0:
                flag=True
                for i in dict:
                    if i%5!=0:flag=False
                if flag:
                    return True
            if x%7==0:
                flag=True
                for i in dict:
                    if i%7!=0:flag=False
                if flag:
                    return True
            return False

    装逼写法

    from collections import Counter
    from math import gcd
    class Solution:
        def hasGroupsSizeX(self, deck: List[int]) -> bool:
            return reduce(gcd,Counter(deck).values())>=2
  • 相关阅读:
    索引压缩
    拼写校正
    词典(词汇表)
    Text Relatives II
    Text Relatives
    CoreText
    Quartz2D Text
    PDF Document Creation, Viewing
    Core Graphics Layer Drawing
    Bitmap Images and Image Masks
  • 原文地址:https://www.cnblogs.com/xxxsans/p/13553033.html
Copyright © 2011-2022 走看看