LeetCode 0229 - Majority Element II

2021-08-11 11:57:55 浏览数 (1)

Majority Element II

Desicription

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

Note: The algorithm should run in linear time and in O(1) space.

Example 1:

代码语言:javascript复制
Input: [3,2,3]
Output: [3]

Example 2:

代码语言:javascript复制
Input: [1,1,1,3,3,2,2,2]
Output: [1,2]

Solution

代码语言:javascript复制
class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        int number_1 = 0;
        int number_2 = 1;
        int count_1 = 0;
        int count_2 = 0;
        for(auto num : nums) {
            if(num == number_1) {
                count_1  ;
            } else if(num == number_2) {
                count_2  ;
            } else if(count_1 == 0) {
                count_1 = 1;
                number_1 = num;
            } else if(count_2 == 0) {
                count_2 = 1;
                number_2 = num;
            } else {
                count_1--;
                count_2--;
            }
        }
        count_1 = 0;
        count_2 = 0;
        for(auto num : nums) {
            if(num == number_1) {
                count_1  ;
            } else if(num == number_2) {
                count_2  ;
            }
        }
        vector<int> result;
        if(count_1 > nums.size() / 3) {
            result.push_back(number_1);
        }
        if(count_2 > nums.size() / 3) {
            result.push_back(number_2);
        }
        return result;
    }
};

0 人点赞