题解 | #牛的体重统计#

牛的体重统计

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

全部评论

相关推荐

2 1 评论
分享
牛客网
牛客企业服务