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 ⌋ 次的元素。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52356817

原文

给定一个长度为n的整型数组,找出所有出现超过 ⌊ n/3 ⌋ 次的元素。算法应该运行在线性时间上,且进用O(1)空间。

提示:

它可能有多少个主要元素?

原文

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?

分析

The Boyer-Moore Vote Algorithm solves the majority vote problem in linear time O(n)and logarithmic space O(logn). The majority vote problem is to determine in any given sequence of choices whether there is a choice with more occurrences than half of the total number of choices in the sequence and if so, to determine this choice. Note how this definition contrasts with the mode in which it is not simply the choice with the most occurrences, but the number of occurrences relative to the total length of the sequence.

Mathematically, given a finite sequence (length n) of numbers, the object is to find the majority number defined as the number that appears more than n/2 times.

import java.util.*;
public class MajorityVote {
     public int majorityElement(int[] num) {
         int n = num.length;
         int candidate = num[0], counter = 0;
         for (int i : num) {
             if (counter == 0) {
                 candidate = i;
                 counter = 1;
             } else if (candidate == i) {
                 counter++;
             } else {
                 counter--;
             }
        }

        counter = 0;
        for (int i : num) {
            if (i == candidate) counter++;
        }
        if (counter <= n / 2) return -1;
        return candidate;
    }
    public static void main(String[] args) {
        MajorityVote s = new MajorityVote();
        System.out.format("%d\n", s.majorityElement(new int[] {1, 2, 3}));
        System.out.format("%d\n", s.majorityElement(new int[] {2, 2, 3}));
    }
}

代码

Java

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> marjority = new ArrayList<>();
        int n = nums.length;
        int candidate1 = 0, candidate2 = 0, counter1 = 0, counter2 = 0;
        for (int i : nums) {
            if (candidate1 == i) {
                counter1++;
            } else if (candidate2 == i) {
                counter2++;
            } else if (counter1 == 0) {
                candidate1 = i;
                counter1 = 1;
            } else if (counter2 == 0) {
                candidate2 = i;
                counter2 = 1;
            } else {
                counter1--;
                counter2--;
            }
        }
        counter1 = 0;
        counter2 = 0;
        for (int i : nums) {
            if (i == candidate1) counter1++;
            else if (i == candidate2) counter2++;
        }
        if (counter1 > n/3) marjority.add(candidate1);
        if (counter2 > n/3) marjority.add(candidate2);
        return marjority;
    }
}
目录
相关文章
|
1月前
|
算法 Java
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
23 1
|
1月前
|
算法 C语言
【C语言】Leetcode 27.移除元素
【C语言】Leetcode 27.移除元素
21 0
【C语言】Leetcode 27.移除元素
|
1月前
|
C++
两种解法解决 LeetCode 27. 移除元素【C++】
两种解法解决 LeetCode 27. 移除元素【C++】
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
1月前
|
算法
LeetCode[题解] 1261. 在受污染的二叉树中查找元素
LeetCode[题解] 1261. 在受污染的二叉树中查找元素
16 1
|
5天前
|
算法
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
9 3
|
8天前
|
算法
【力扣】169. 多数元素
【力扣】169. 多数元素
|
1月前
|
存储 JavaScript
leetcode82. 删除排序链表中的重复元素 II
leetcode82. 删除排序链表中的重复元素 II
22 0
|
1月前
leetcode83. 删除排序链表中的重复元素
leetcode83. 删除排序链表中的重复元素
10 0
|
1月前
|
算法 搜索推荐
LeetCode刷题---215. 数组中的第K个最大元素(双指针,快速选择)
LeetCode刷题---215. 数组中的第K个最大元素(双指针,快速选择)

热门文章

最新文章