Python, LintCode, 46. Majority Element

class Solution:
    """
    @param: nums: a list of integers
    @return: find a  majority number
    """
    def majorityNumber(self, nums):
        # write your code here
        if len(nums) == 0:
            return None
        ht = {}
        for i in range(len(nums)):
            if nums[i] in ht:
                ht[nums[i]] += 1
            else:
                ht[nums[i]] = 1
        val = list(ht.values())
        return list(ht.keys())[val.index(max(val))]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章