腾讯五十题 No.36 多数元素

题目链接


既然个数大于一般那就先sort,再取中间吧

但是面试官可能会想要其答案

class Solution {
    public int majorityElement(int[] nums) {
        int count = 1,maj = nums[0];
        for(int i = 1;i < nums.length;i++){
            if(maj==nums[i]){
                count++;
            }else{
                count--;
                if(count == 0){
                    maj = nums[i+1];
                }
            }
        }
        return maj;
    }
}

posted @   蹇爱黄  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示