标签:hal numbers code map targe pre get ret empty
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if(numbers.empty()){
return 0;
}
if(numbers.size() == 1){
return numbers[0];
}
int len = (int)numbers.size();
float target = len/2.0;
map<int, int> m;
for(int num: numbers){
if(m.count(num) != 0){
m[num]++;
if(m[num] > target) return num;
}
else{
m.insert(pair<int, int>(num, 1));
}
}
return 0;
}
};
标签:hal numbers code map targe pre get ret empty
原文地址:https://www.cnblogs.com/theodoric008/p/9560275.html