[哈希] 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; } };
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号