#leetcode#Majority Element II

本文介绍了一种线性时间和常数空间复杂度内找到数组中出现次数超过n/3的元素的方法。通过使用两个计数器来跟踪不同可能的超多数元素,确保最终结果的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

  1. How many majority elements could it possibly have?

学习了曹神的思路:http://meetqun.com/thread-10186-1-1.html

找出出现次数严格大于[n/3]的数。* S: y/ B6 W# _* y# r
分析: 简单题。这种数最多有两个。其实我们每次扔掉3个不同的数,结果是不变的——就像众数的推广。" j8 m5 s* D* x6 {. l; C; k. y
如何扔掉3个不同的数?0 d, L2 n7 R4 W
用两个“槽”保存两个不同的数,遍历所有的数,如果和槽里的数相等,则计数器增加1。否则把两个槽里的数和这个数一起扔掉,正好是3个不同的数。
注意槽可能为空(计数器为0),最后两个槽里的数(如果有)就是可能的候选。我们再数一下分别出现了多少次,看看是否能达到要求。因为只有两个候选,数一下时间也是O(n)的。, P/ j3 @% g6 l4 A) T' m$ ~
总体时间复杂度是O(n)的,空间复杂度是O(1)的。关键在于对空槽的处理——不过最后数的时候,无论空不空我都数了,这没关系……

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        int num1 = 0;
        int num2 = 0;
        int count1 = 0;
        int count2 = 0;
        
        for(int num : nums){
            if(num == num1){
                count1++;
            }else if(num == num2){
                count2++;
            }else if(count1 == 0){
                num1 = num;
                count1 = 1;
            }else if(count2 == 0){
                num2 = num;
                count2 = 1;
            }else{
                count1--;
                count2--;
            }
        }
        count1 = 0;
        count2 = 0;
        for(int i : nums){
            if(i == num1){
                count1++;
            }else if(i == num2){
                count2++;
            }
        }
        
        List<Integer> res = new ArrayList<Integer>();
        if(count1 > nums.length / 3){
            res.add(num1);
        }
        if(count2 > nums.length / 3){
            res.add(num2);
        }
        
        return res;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值