首页 > 编程语言 > 详细

LeetCode Majority Element Python

时间:2015-03-27 10:39:44      阅读:189      评论:0      收藏:0      [点我收藏+]

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.

 

 

 1 class Solution:
 2     # @param num, a list of integers
 3     # @return an integer
 4     def majorityElement(self, num):
 5         dic = {}
 6         for i in range(len(num)):
 7             if (num[i]) not in dic:
 8                 dic[num[i]] = 1
 9             else:
10                 dic[num[i]] += 1
11         l = [(a,b) for a, b in dic.items()]
12         return (sorted(l,key = lambda x:x[1]))[-1][0]
13         

 

LeetCode Majority Element Python

原文:http://www.cnblogs.com/fyymonica/p/4370966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!