leetcode-mid-others-169. Majority Element¶

mycode  54.93%

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dic = {}
        count = len(nums) // 2
        for item in nums:
            dic[item] = dic.get(item,0) + 1
            if dic[item] > count :
                 return item

参考:

思路 : 题目中说明了majority的特点,所以排序之后从下标就能看出来啦

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return nums[len(nums)//2]
原文地址:https://www.cnblogs.com/rosyYY/p/10984216.html