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)
原文地址:https://www.cnblogs.com/panweiwei/p/14025027.html