题解 | #牛的体重统计#

牛的体重统计

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

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

一、题目考察的知识点

map计数

二、题目解答方法的文字分析

很久没写map了,导致都忘记怎么使用迭代器了,还好边查阅资料边回忆,边写边回忆,还是想起怎么写了。模板题,看过就会写

三、本题解析所用的编程语言

c++

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:20
点赞 评论 收藏
分享
真烦好烦真烦:豆包润色了自己没看看吗,再说了,都说豆包是愚蠢且勤快的大学生,ds才是聪明的研究生,怎么敢让豆包写论文的
你们的毕业论文什么进度了
点赞 评论 收藏
分享
07-07 17:06
已编辑
深圳技术大学 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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