其他分享
首页 > 其他分享> > Leetcode-多数元素

Leetcode-多数元素

作者:互联网

在这里插入图片描述

一道简单题直接把今日KPI拉满了,做完直接消除emo,hhh
Python的思路就很简单啊,建个字典,把各数出现的次数全部统计到字典里,最后判断一下,over~~

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        k = int(len(nums)/2)
        d = dict()
        for i in nums:
            if(i in d):
                d[i] = d[i]+1
            else:
                d[i] = 1
        for i in d:
            if(d[i]>k):
                return i

运行结果:
在这里插入图片描述

看了下这题的评论。。。。仿佛不是同一道题。。。。

标签:nums,int,over,元素,这题,多数,Leetcode,各数,字典
来源: https://blog.csdn.net/weixin_44166338/article/details/123584131