class Solution {
public:
int majorityElement(vector<int>& nums) {
int number = nums.size() / 2 + 1;
unordered_map<int, int> uomap;
int res = 0;
for(auto v : nums){
uomap[v]++;
}
for(auto ech : uomap){
if(ech.second >= number){
res = ech.first;
return res;
}
}
return res;
}
};
数组中出现次数超过一半的数字-哈希表(剑指offer39)-C++
于 2021-03-28 21:16:04 首次发布