169.Majority Element

 

 

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]
        dic = {}
        n = len(nums) // 2
        for i in range(len(nums)):
            tmp = nums[i]
            if tmp in dic:
                dic[tmp] += 1
                if dic[tmp] > n:
                    return tmp
            else:
                dic[tmp] = 1

 

posted @ 2020-05-09 14:54  星海寻梦233  阅读(58)  评论(0编辑  收藏  举报