LeetCode 781. 森林中的兔子 (思维、数学)

    技术2026-01-16  8

    森林中的兔子 这个有点像坐船问题哈。

    class Solution { public: int numRabbits(vector<int>& a) { int ans = 0; unordered_map<int,int> mp; for(int x:a){ mp[x+1]++; } for(auto it:mp){ int x = it.first,y = it.second; if(y%x){ ans += (y/x+1)*x; }else{ ans += y/x*x; } } return ans; } };
    Processed: 0.028, SQL: 9