【Leetcode】【Python】169. Majority

2017-10-21  本文已影响0人  小歪与大白兔

问题描述:

Paste_Image.png

代码示例:

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n == 1 : return nums[0]
        dd = {}
        result = []
        for index, value in enumerate(nums):
            if value in dd:
                dd[value] += 1
                if dd[value] > n//2:
                    result =value
            else :
                dd[value] = 1
        return result
上一篇下一篇

猜你喜欢

热点阅读