lettcode算法设计题914--卡片分组

这个算法我已经在lettcode上面提交了,已通过,后来我将代码也添加了注释,如果还有看不懂的地方欢迎留言。

from collections import Iterable
class Solution(object):
    def get_max_number(self, num1, num2):
        while num1 != num2:
            if num1>num2:
                num1=num1-num2
            else:
                num2=num2-num1
        return num1
    def hasGroupsSizeX(self, deck):
        signal = set()
        statistic=list()
        if isinstance(deck, Iterable):
            for i in deck:
                #将给定列表参数出现的数都存入集合中
                signal.add(i)
            for x in signal:
                #将每个元素在deck中出现的次数存入statistic列表中
                statistic.append(deck.count(x))
        else:
            print('给定数组参数错误')
            return
        if len(statistic) != 1 :
            #当desk中出现不重复的元素时
            for y in range(len(statistic)):
                if y ==0 :
                    min_number = self.get_max_number(statistic[0],statistic[1])
                else:
                    min_number = self.get_max_number(statistic[y],min_number)
            print(min_number)
            if min_number >=2:
                for x in statistic:
                    if x % min_number==0:
                        return True
                    else:
                        return False
            else:
                return False
        elif statistic[0]>=2 and len(statistic)==1:
            #当desk中只出现多次同样的元素时
            return True
        else:
            #当desk是由单个元素组成时
            return False

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值