781. 森林中的兔子




class Solution(object):
    def numRabbits(self, answers):
        """
        :type answers: List[int]
        :rtype: int
        """
        if not answers:
            return 0
        mydict = {}
        for item in answers:
            if item in mydict.keys():
                mydict[item] += 1
            else:
                mydict[item] = 1
        # print(mydict)
        res = 0
        for k, v in mydict.items():
            res += ((v - 1) / (k + 1) + 1) * (k + 1)
        return int(res)
posted @ 2020-11-23 16:06  人间烟火地三鲜  阅读(130)  评论(0编辑  收藏  举报