美文网首页
169. Majority Element

169. Majority Element

作者: JERORO_ | 来源:发表于2018-06-28 20:00 被阅读0次

问题描述

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exists in the array.

思路

很慢 但是很好理解的解法
后续再做追求速度的

  1. sort一遍list
  2. return 那个list的中间数
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return nums[len(nums)//2]

后续

降低time complexity

相关文章

网友评论

      本文标题:169. Majority Element

      本文链接:https://www.haomeiwen.com/subject/lmpnyftx.html