leetcode -- Majority Element -- 简单,但是还有很多其他方法

来源:互联网 发布:linux 查看文件命令 编辑:程序博客网 时间:2024/06/05 20:23

https://leetcode.com/problems/majority-element/

import mathclass Solution(object):    def majorityElement(self, nums):        """        :type nums: List[int]        :rtype: int        """        mydict = {}        for x in nums:            if x in mydict:                mydict[x] += 1            else:                mydict[x] = 1        target = math.ceil(float(len(nums))/2)        for k,v in mydict.items():            if v >= target:                return k
0 0
原创粉丝点击