class Solution { public: int MoreThanHalfNum_Solution(vector<int> numbers) { unordered_map<int,int> m; int n = numbers.size(); for(int i = 0;i<n;++i) { m[numbers[i]]++; if(m[numbers[i]] > n/2) return numbers[i]; } return -1; } };
思路:使用哈希表
class Solution { public: int MoreThanHalfNum_Solution(vector<int> numbers) { unordered_map<int,int> m; int n = numbers.size(); for(int i = 0;i<n;++i) { m[numbers[i]]++; if(m[numbers[i]] > n/2) return numbers[i]; } return -1; } };
思路:使用哈希表