169. Majority Element

2018-06-28  本文已影响0人  JERORO_

问题描述

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

上一篇 下一篇

猜你喜欢

热点阅读