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

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

import math
class 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

你可能感兴趣的:(LeetCode)