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. yii CFormModel中的rules验证机制

    public function rules() { return array( array('username, password', 'required'), array('rememberMe', ...

  2. LeetCode--125--验证回文串

    问题描述: 好 times out: class Solution(object): def isPalindrome(self, s): """ :type s: st ...

  3. bzoj1834: [ZJOI2010]network 网络扩容 费用流

    bzoj1834 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的情况下,1到N的最大流: 2.将1到N的最大流增加K所需的最小扩容 ...

  4. python-day63--前端

    一. 响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等 - 响应式怎么实现的? 1. CSS3 media query 媒体查询 2. JS去控制网页的布局和样式等 - 缺 ...

  5. python-day27--configparser模块

    1.来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forwa ...

  6. php表单提交安全方法

    1.$_SERVER["PHP_SELF"] 将表单数据发送到页面本身,而不是跳转到另一张页面.这样,用户就能够在表单页面获得错误提示信息.2.通过使用 htmlspecialch ...

  7. 页面跳转 Server.Transfer和 Response.Redirect的区别

    1.Server.Transfer 用于把处理的控制权从一个页面转移到另一个页面,在转移的工程中没有离开服务器内部控件(如request,session等)保存的信息不变.因此你能从a页面跳转到b页面 ...

  8. quartz---触发job时间和结束时间

    quartz:Trigger:触发job时间和结束时间 package com.imooc.demo.helloQuartz; import java.text.SimpleDateFormat; i ...

  9. echatrs可视化图在隐藏后显示不出来或是宽度出现问题

    最近在做一个可视化的项目,用了百度的ECharts.js作为可视化的视图框架,echarts的实例很多,基本能满足项目的需求,而且文档也相对完整.清晰,是个很不错的前端可视化框架. 我们的项目是使用b ...

  10. 068——VUE中vuex的使用场景分析与state购物车实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...