问题描述
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.
思路
很慢 但是很好理解的解法
后续再做追求速度的
- sort一遍list
- return 那个list的中间数
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return nums[len(nums)//2]
后续
降低time complexity
网友评论