题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
class Solution { public: int MoreThanHalfNum_Solution(vector<int> numbers) { int count=1; int val=numbers[0]; for(int i=1;i<numbers.size();i++) { if(numbers[i]==val) { count++; }else { if(count==1) { val=numbers[i]; }else { count--; } } } return val; } };