Majority Element (leetcode )

来源:互联网 发布:河南数据统计采集门户 编辑:程序博客网 时间:2024/04/30 08:01

题目:

          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:
             # @param num, a list of integers
             # @return an integer
             def majorityElement(self, num):
                 n = len(num)
                 num.sort()
                 return num[n/2]
        



0 0
原创粉丝点击