面试题39:数组中出现次数超过一半的数字 面试题39:数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,找出这个数字。 // 摩尔投票法 class Solution { public: int majorityElement(vector<int>& nums) { int x,votes; for(int num:nums) { if(votes==0) x = num; votes += num==x? 1:-1; // votes += (num==x? 1:-1) } return x; } };