题目考察的知识点:哈希
题目解答方法的文字分析:遍历两个数组,将数据插入到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;
}
};

京公网安备 11010502036488号