题解 | #不同的体重#

不同的体重

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;
    }
};

全部评论

相关推荐

06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务