LintCode:主元素 III

LintCode:主元素 III

Python

class Solution:
    """ @param nums: A list of integers @param k: As described @return: The majority number """
    def majorityNumber(self, nums, k):
        # write your code here
        d = {}
        m = len(nums)
        for num in nums:
            if num not in d:
                d[num] = 1
            else:
                d[num] += 1
        for key in d:
            if (1.0 * d[key]) / m > 1.0 / k:
                return key

你可能感兴趣的:(LintCode:主元素 III)