题解 | #不同的体重#
不同的体重
https://www.nowcoder.com/practice/4a6411ef749445e88baf7f93d1458505
考察知识点:哈希
题目分析:
可以使用两个哈希表,一个用来记录每种体重的牛的数量,另一个用来检查数量是否是不同的。
所用编程语言:C++
#include <unordered_map>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型vector
* @return bool布尔型
*/
bool uniqueOccurrences(vector<int>& arr) {
// write code here
unordered_map<int, int> mp;
unordered_map<int, int> nummp;
for (auto &weight: arr) {
mp[weight]++;
}
for (auto &num: mp) {
if (nummp.count(num.second)) {
return false;
} else nummp[num.second]++;
}
return true;
}
};
查看1道真题和解析