LeetCode每日一题:主要元素(hash)

开一个hash,只要元素个数大于一半就可以返回
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
unordered_map<int, int> hash;
for (auto &t : nums)
{
hash[t] ++ ;
if (hash[t] > n / 2) return t;
}
return -1;
}
};
Acwing每日一题:
在这里插入代码片