题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
class Solution { public: int MoreThanHalfNum_Solution(vector<int> numbers) { int half = numbers.size() / 2; map<int, int> mp; int maxNumb = 0; for(int i = 0; i < numbers.size(); i++) { mp[numbers[i]]++; if(mp[numbers[i]] > maxNumb) { maxNumb = mp[numbers[i]]; if(mp[numbers[i]] > half) return numbers[i]; } } return 0; } };