Leetcode 229. Majority Element II

Leetcode 229. Majority Element II

Given an integer array of size n, find all elements that appear more than n/3 ⌊ n / 3 ⌋ times. The algorithm should run in linear time and in O(1) space.

The algorithm we applied in this problem named Boyer-Moore Majority Vote algorithm.

There is a tricky point in this solution, that is the num_1 and num_2 cannot guarantee be the number we want. Therefore, we need to check if num_1 and num_2 appear more than n/3 ⌊ n / 3 ⌋ times in the whole array (from line 34 to line 41). On the other hand, what this algorithm can guarantee is that if a number appears more than n/3 ⌊ n / 3 ⌋ times, then it must be in the num_1 or num_2 .

这道题目采用的是Boyer-Moore Majority Vote算法,其中主要的一个问题是num_1num_2里面保存的究竟是什么?简单点说,如果一个数在数组中出现的次数超过了 n/3 ⌊ n / 3 ⌋ 次,那么它一定会被保存到num_1num_2中。但是反过来,就不成立了,num_1num_2中的数不一定出现的次数不一定超过了 n/3 ⌊ n / 3 ⌋ 次。这就是为什么需要检查num_1num_2在数组中出现的次数。

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums)
    {
        vector<int> ret;

        int cnt_1 = 0, cnt_2 = 0;
        int num_1 = -1, num_2 = -1;
        int size = nums.size();

        for(int i = 0; i < size; i++)
        {
            if(num_1 == nums[i])
                cnt_1++;
            else if(num_2 == nums[i])
                cnt_2++;
            else if(cnt_1 == 0)
            {
                cnt_1++;
                num_1 = nums[i];
            }
            else if(cnt_2 == 0)
            {
                cnt_2++;
                num_2 = nums[i];
            }
            else
            {
                cnt_1--;
                cnt_2--;
            }
        }

        cnt_1 = cnt_2 = 0;
        for(int i = 0; i < size; i++)
        {
            if(nums[i] == num_1)
                cnt_1++;
            else if(nums[i] == num_2)
                cnt_2++;
        }

        if(cnt_1 > size / 3)
            ret.push_back(num_1);
        if(cnt_2 > size / 3)
            ret.push_back(num_2);

        return ret;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值