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.
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: list[int]
:rtype: int
"""
nums.sort()
return nums[len(nums)/2]

本文介绍了一种简单有效的方法来找出数组中的多数元素,即出现次数超过数组长度一半的元素。通过排序数组并返回中间位置的元素即可找到多数元素。
139

被折叠的 条评论
为什么被折叠?



