其他分享
首页 > 其他分享> > Boyer-Moore 数组中出现次数超过一半的数字

Boyer-Moore 数组中出现次数超过一半的数字

作者:互联网

 

Boyer-Moore 

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int candidate = -1;
        int count = 0;
        for (int num : nums) {
            if (num == candidate)
                ++count;
            else if (--count < 0) {
                candidate = num;
                count = 1;
            }
        }
        return candidate;
    }
};

  

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/solution/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-pvh8/

 

标签:count,ci,Moore,candidate,int,num,数组,Boyer,shu
来源: https://www.cnblogs.com/rsapaper/p/16217226.html