题解 | #牛的体重统计#

牛的体重统计

https://www.nowcoder.com/practice/15276ab238c9418d852054673379e7bf

考察知识点:哈希

题目分析:

分别遍历一遍数组,记录每个体重出现的次数,然后维护出现次数的最大值即可。

所用编程语言:C++

#include <unordered_map>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param weightsA int整型vector 
     * @param weightsB int整型vector 
     * @return int整型
     */
    int findMode(vector<int>& weightsA, vector<int>& weightsB) {
        // write code here
        unordered_map<int, int> mp;
        int res = 0;
        for (auto weight: weightsA) {
            mp[weight]++;
            if (mp[weight] >= mp[res]) 
                res = mp[weight] == mp[res] ? max(res, weight) : weight;
        }
        for (auto weight: weightsB) {
            mp[weight]++;
            if (mp[weight] >= mp[res]) 
                res = mp[weight] == mp[res] ? max(res, weight) : weight;
        }
        return res;
    }
};

全部评论

相关推荐

明天不下雨了:这个项目 这个简历 这个模板 莫不是一个开源的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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