美文网首页
229. Majority Element II

229. Majority Element II

作者: FlynnLWang | 来源:发表于2016-12-27 23:23 被阅读0次

Question

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.
Hint:
How many majority elements could it possibly have?

Code

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> result = new ArrayList<>();
        if (nums == null || nums.length == 0) return result;
        if (nums.length == 1) {
            result.add(nums[0]);
            return result;
        }
        
        int n1 = nums[0], n2 = 0, c1 = 1, c2 = 0;
        for (int i = 1; i < nums.length; i++) {
            int n = nums[i];
            if (n == n1) {
                c1++;
            } else if (n == n2) {
                c2++;
            } else if (c1 == 0) {
                n1 = n;
                c1++;
            } else if (c2 == 0) {
                n2 = n;
                c2++;
            } else {
                c1--;
                c2--;
            }
        }
        
        c1 = 0;
        c2 = 0;
        
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == n1) c1++;
            else if (nums[i] == n2) c2++;
        }
        if (c1 > nums.length / 3) result.add(n1);
        if (c2 > nums.length / 3) result.add(n2);
        
        return result;
    }
}

Solution

摩尔投票法。
数组中至多可能会有2个出现次数超过 ⌊ n/3 ⌋ 的众数
记变量n1, n2为候选众数; c1, c2为它们对应的出现次数
遍历数组,记当前数字为num
若num与n1或n2相同,则将其对应的出现次数加1
否则,若c1或c2为0,则将其置为1,对应的候选众数置为num
否则,将c1与c2分别减1
最后,再统计一次候选众数在数组中出现的次数,若满足要求,则返回之。

相关文章

  • II Boyer-Moore Majority Vote Alg

    229. Majority Element II Given an integer array of size n...

  • 229. Majority Element II

    先排序 然后顺序扫描,需要注意边缘,代码如下: 但是这道题目限制了时间是限行时间空间是o(1),因此,既不能使用排...

  • 229. Majority Element II

    Given an integer array of size n, find all elements that ...

  • 229. Majority Element II

    题目 题目很简短,给定一个包含n个数字的数组,找出所有出现次数超过 ⌊ n/3 ⌋ 次的数。要求O(n)时间复杂度...

  • 229. Majority Element II

    思路就是三个三个删,如何描述是三个三个删的关键是两个count要描述好。之前没仔细写,count应该有四种case...

  • 229. Majority Element II

    Question Given an integer array of size n, find all eleme...

  • 229. Majority Element II

    https://leetcode.com/problems/majority-element-ii/ 给定一个数组...

  • 229. Majority Element II

    给定一个大小为 n 的整数数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 摩尔投票法 大于 n/3 的数最...

  • 229. Majority Element II (M)

    Given an integer array of size n, find all elements that ...

  • Majority Element II

    Majority Element II 今天是一到有关数学计算的题目,是Majority Element(回复01...

网友评论

      本文标题:229. Majority Element II

      本文链接:https://www.haomeiwen.com/subject/cunevttx.html