[哈希] leetcode 781 Rabbits in Forest

Posted fish1996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[哈希] leetcode 781 Rabbits in Forest相关的知识,希望对你有一定的参考价值。

problem:https://leetcode.com/problems/rabbits-in-forest/

        值为n的可以和其它n + 1个值为n的成组,统计每个值出现的次数,看它们可以组成多少组相同颜色的兔子,然后乘以组中兔子个数。

class Solution 
public:
    int numRabbits(vector<int>& answers) 
        unordered_map<int,int> counts;
        for(int i = 0; i < answers.size(); i++)
        
            counts[answers[i]]++;
        
        int res = 0;
        for(auto& count : counts)
        
            int expect = count.first + 1;
            int number = count.second;
            res += ceil((float)number / expect) * expect;
        
        return res;
    
;

 

以上是关于[哈希] leetcode 781 Rabbits in Forest的主要内容,如果未能解决你的问题,请参考以下文章

LWC 71: 781. Rabbits in Forest

781. Rabbits in Forest

781. Rabbits in Forest

781. Rabbits in Forest

[Math_Medium] 781. Rabbits in Forest

leetcode中等781森林中的兔子