[LeetCode] Majority Element II

简介: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.解题思路摩尔投票法。投票法的核心是找出两个候选众数进行投票,需要两遍遍历

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

解题思路

摩尔投票法。投票法的核心是找出两个候选众数进行投票,需要两遍遍历,第一遍历找出两个候选众数,第二遍遍历重新投票验证这两个候选众数是否为众数即可。

实现代码

C++:

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        vector<int> res;
        int m = 0, n = 0, cm = 0, cn = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == m)
            {
                cm++;
            }
            else if (nums[i] == n)
            {
                cn++;
            }
            else if (cm == 0)
            {
                m = nums[i];
                cm = 1;
            }
            else if (cn == 0)
            {
                n = nums[i];
                cn = 1;
            }
            else
            {
                --cm;
                --cn;
            }
        }

        cm = cn = 0;
        for (auto& a : nums)
        {
            if (a == m)
            {
                cm++;
            }
            else if (a== n)
            {
                cn++;
            }
        }
        if (cm > nums.size() / 3)
        {
            res.push_back(m);
        }
        if (cn > nums.size() / 3)
        {
            res.push_back(n);
        }

        return res;
    }
};

Java:

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        int m = 0, n = 0, cm = 0, cn = 0;
        for (int a : nums) {
            if (a == m) {
                ++cm;
            } else if (a == n) {
                ++cn;
            } else if (cm == 0) {
                m = a;
                cm = 1;
            } else if (cn == 0) {
                n = a;
                cn = 1;
            } else {
                --cm;
                --cn;
            }
        }

        cm = cn = 0;
        for (int a : nums){
            if (a == m) {
                ++cm;
            } else if (a == n) {
                ++cn;
            }
        }

        if (cm > nums.length / 3) {
            res.add(m);
        }
        if (cn > nums.length / 3) {
            res.add(n);
        }

        return res;
    }
}
目录
相关文章
|
6月前
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
47 1
|
算法 Python
LeetCode 169. 多数元素 Majority Element
LeetCode 169. 多数元素 Majority Element
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
80 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
算法
LeetCode 229. Majority Element II
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
61 0
LeetCode 229. Majority Element II
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
75 0
LeetCode 162. Find Peak Element
|
Java C++
LeetCode之Remove Element
LeetCode之Remove Element
87 0
LeetCode之Next Greater Element I
LeetCode之Next Greater Element I
60 0
|
算法 Java
LeetCode 229 Majority Element II(主要元素II)(Array)(Boyer–Moore majority vote algorithm)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52356817 原文 给定一个长度为n的整型数组,找出所有出现超过 ⌊ n/3 ⌋ 次的元素。
1006 0
|
容器
LeetCode 169 Majority Element(主要元素)(vector、map)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50504698 翻译 给定一个长度为n的数组,找出主要的元素。
783 0