题解 | #草原上优势牛种#
草原上优势牛种
https://www.nowcoder.com/practice/178705f48adc4e39ac8537a22e8941cd
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @return int整型
*/
int majority_cow(vector<int>& nums)
{
// write code here
unordered_map<int,int> data;
for (auto it : nums)
{
data[it]++;
}
int result;
for (auto it : data)
{
if (it.second >= nums.size() / 2)
{
result = it.first;
break;
}
}
for (auto it : data)
{
if (it.second > data[result])
{
result = it.first;
}
}
return result;
}
};
使用哈希
查看10道真题和解析