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 exist in the array.
EXAMPLE: Input: [2,2,1,1,1,2,2] Output: 2
求列表个数中大于n/2的数值。
class Solution:
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
#1常规统计个数
nums_len = len(nums)
for i in set(nums):
if nums.count(i)>nums_len/2:
return i
#2,找小到大排序后n/2位置的值
#return sorted(nums)[len(nums)//2]