LeetCode Majority Element Python
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的更多相关文章
- 2016.5.18——leetcode:Majority Element
Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- [LeetCode] Majority Element II 求大多数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I
原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...
- LeetCode——Majority Element
在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...
- 169. Majority Element@python
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- spring boot:创建一个简单的web(maven web project)
1.新建一个maven web project; 2.在pom.xml文件中添加相应的依赖包: 3.新建一个HelloController请求控制类: 4.编写index.jsp页面: 5.编写启动类 ...
- English trip -- Review Unit7 Shopping 购物
Xu言: 今天,lamb老师帮我梳理的时候到时提醒了我件事,之前Jade老师也说过每个单元的课程其实有个大主题,我需要把这个单元上完以后全部好好的回顾,然后整理一下.把每个单元的主题以及主题(them ...
- Confluence 6 LDAP 用户组结构设置
用户组对象类(Group Object Class) 这是在 LDAP 用户组对象中使用的类的名字.例如: groupOfUniqueNames group 用户组对象过滤器(Group Object ...
- PHP函数总结 (五)
<?php /** * 回调函数: * 指调用函数时并不是传递一个标准的变量作为参数,而是将另一个函数作为参数传递到调用的函数中 * 使用回调函数可以 将一段自己定义的功能传到函数内部使用 * ...
- 二分求LIS并打印结果
1275: God's ladder [DP] 时间限制: 1 Sec 内存限制: 128 MB Special Judge 题目描述 天明来到神之宫殿,在他眼前出现了若干个石柱,每个石柱上有1枚金 ...
- OAF 动态创建组件以及动态绑定属性
在开发中,我们遇到以下一个需求. 一个表格左侧有5列是固定存在的,右侧有N列是动态生成的,并且该N列中第一列可输入,第二列是不可编辑的,但是是数字,如果小于0,那么就要显示为红色,重点标识出来. 首先 ...
- kill word out e ef en em
1● e 2● ef 出,出来 3● en 4● em 使~进入状态,包围,进入~之中
- System.Web.Caching
System.Web.Caching简单封装类: using System; using System.Collections.Generic; using System.Web.Caching; u ...
- vue项目启动
这篇文章主要用于有源码vue项目安装: 1.安装node.js环境(npm包管理器)前面博客有写到如何安装: 2.vue-cli 脚手架构建工具前面博客有写到如何安装: 3.cnpm npm的淘宝镜 ...
- 栈(stack),C++模板实现
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...