781. 森林中的兔子
题目链接:781. 森林中的兔子
代码如下:
class Solution {
public:int numRabbits(vector<int>& answers) {unordered_map<int, int> cnt;for (int x : answers) {cnt[x]++;}int res = 0;for (auto it : cnt) {//如果有 c 只兔子都回答了 x,那么每只兔子都在大小为 x+1 的颜色组中,最少有x+1个颜色组。所以c/x+1向上取整 颜色组。所以c只兔子都回答了 x会让答案增加res += (it.first + it.second) / (it.first + 1) * (it.first + 1);}return res;}
};