题解 | #牛的体重统计#

牛的体重统计

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

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

全部评论

相关推荐

有气魄的马来熊在摸鱼:我爱vivo 马上换手机 vivo我爱你!!!
点赞 评论 收藏
分享
面了100年面试不知...:太礼貌,还是
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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