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):
dic = {}
for i in range(len(num)):
if (num[i]) not in dic:
dic[num[i]] = 1
else:
dic[num[i]] += 1
l = [(a,b) for a, b in dic.items()]
return (sorted(l,key = lambda x:x[1]))[-1][0]

LeetCode Majority Element Python的更多相关文章

  1. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  2. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  5. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  6. [LeetCode] Majority Element 求大多数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. LeetCode Majority Element I

    原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...

  8. LeetCode——Majority Element

    在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...

  9. 169. Majority Element@python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. GetTextAndImageCreateExamPaper

    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA&quo ...

  2. Confluence 6 选项 1 – 在 Confluence 中手动重建用户和用户组

    当你只有少量的用户和用户组的时候,使用这个方法. 使用 Confluence 的系统管理员登录 Confluence. 进入用户目录管理界面,然后移动 内部目录(internal directory) ...

  3. HDU-1163 Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. postgresql相关开源软件及架构简介

    1.PgBouncerPG数据库的一个轻量级连接池工具,功能及特点如下:1)缓存后端PG数据库的连接,当前端应用请求时,分配连接池中的连接给应用,从而充分利用了系统资源.2)允许应用创建比连接池更多的 ...

  5. OPENSHIFT MYSQL使用Navicat远程连接

    1.安装OpenShift的一个叫RHC的远程管理客户端:https://developers.openshift.com/en/getting-started-windows.html 注意ruby ...

  6. STL标准库-迭代器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 本节主要介绍STL六大部件中的Iterators迭代器. 在语言方面讲,容器是一个class template, 算法是一个仿函 ...

  7. kbmMW 5.06.20试用笔记

    1.kbmMWConfiguration自动备份配置文件的问题还没有修正. 下面是以前写过的内容,再一次在新闻组中提出这个问题: kbmMW提供一个强大的配置信息管理对象,前期译过这个对象的介绍,在使 ...

  8. Axure使用笔记1:如何去除IE中每次“已限制网页运行脚本或ActiveX控件”

    每次在Axure中画原型预览的时候,IE每次都有 这个比较烦,在Internent做如下设置,即可不再烦恼 看到没,给允许活动内容在我的计算机上的文件中运行打上勾

  9. 使用Git进行本地提交后,未上传提交,却不小心删除了本地提交或提交所在分支,怎么办?????

    使用Git进行本地提交后,未上传提交,却不小心删除了本地提交或提交所在分支,怎么办????? 不要紧!!!! 可以使用git reflog命令来帮助恢复删除的本地提交! 运行以下命令你就知道怎么用了! ...

  10. 使用adb命令对手机进行截屏保存到电脑,SDCard

    adb shell /system/bin/screencap -p /sdcard/screenshot.png(保存到SDCard) adb pull /sdcard/screenshot.png ...