题解 | #牛的体重统计#

牛的体重统计

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

题目考察的知识点:哈希

题目解答方法的文字分析:遍历两个数组,将数据插入到map中,然后寻找最大的众数。

本题解析所用的编程语言:c++

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

全部评论

相关推荐

09-25 16:00
门头沟学院 Java
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
09-10 15:05
已编辑
门头沟学院 游戏后端
点赞 评论 收藏
分享
09-25 15:55
门头沟学院 Java
小肥罗:有道理哈哈真实真实
我的秋招日记
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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