题目

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.

代码:oj测试通过 Runtime: 197 ms

 class Solution:
# @param num, a list of integers
# @return an integer
def majorityElement(self, num):
if len(num) == 1:
return num[0] candidate = 0
count = 0
for i in range(len(num)):
if count == 0:
candidate = num[i]
count += 1
else:
if num[i] == candidate :
count += 1
else:
count -= 1
return candidate

思路

这个自己想不出来。上网找的Moore Voting算法。

这个方法在思路上还是比较朴实无华的,但是很精妙。

注意题目的条件,重复出现大于数据长度一半的元素为众数。

因此可以采用一种对冲思路:一旦相邻的两个元素不同,就把这两个元素对冲抵消掉;由于众数的出现频次大于数据其他所有元素出现频次之和,所以这种对冲抵消最后剩下的一定是众数。

之前看过几篇日志说这道题 还不错 记录在下面:

http://www.yanyulin.info/pages/2014/12/851338983752.html

http://www.geeksforgeeks.org/majority-element/

http://bookshadow.com/weblog/2014/12/22/leetcode-majority-element/

leetcode 【 Majority Element 】python 实现的更多相关文章

  1. LeetCode Majority Element Python

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

  2. 2016.5.18——leetcode:Majority Element

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

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

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

  4. [LeetCode] Majority Element 求众数

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

  5. LeetCode Majority Element I && II

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

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

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

  7. [LeetCode] Majority Element 求大多数

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

  8. LeetCode Majority Element I

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

  9. LeetCode——Majority Element

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

  10. 169. Majority Element@python

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

随机推荐

  1. windows添加快速启动栏

    步骤: 右击任务栏——选择“新建工具栏” 在“文件夹”路径中填入%appdata%\Microsoft\Internet Explorer\Quick Launch并单点“选择文件夹” 右键单击任务栏 ...

  2. tomcat服务器用Servlet类查找磁盘文件上的Json信息,如果匹配则在浏览器上显示出该条内容的全部信息

    package com.swift; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOE ...

  3. ES6学习(一):数值的扩展

    chapter06 数值的扩展 6.1 二进制和八进制 二进制 前缀 0b 或者 0B 八进制 前缀 0o 或者 0O 6.2 Number.isFinite() Number.isNaN() 原先这 ...

  4. swiper动画效果

    参考swiper官方网站:http://www.swiper.com.cn/ Swiper常用于移动端网站的内容触摸滑动: 结构展示:   纯javascript打造的滑动特效插件,面向手机.平板电脑 ...

  5. 牛客小白月赛5 I 区间 (interval) 【前缀和】

    链接:https://www.nowcoder.com/acm/contest/135/I 题目描述 Apojacsleam喜欢数组. 他现在有一个n个元素的数组a,而他要对a[L]-a[R]进行M次 ...

  6. MySQL - UNION 和 UNION ALL 操作符

    UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SE ...

  7. 嵌入式开发 centos7 交叉编译环境准备

    1. 安装centos7,启动图像化界面. 参考:https://blog.csdn.net/qq_23014435/article/details/74347925 # systemctl get- ...

  8. ThinkPHP函数I代码优化

    ThinkPHP/Common/common.php 文件 I函数,主要用来获取一些gpc请求的变量的,函数有一部分代码是过滤变量的,每次都运行一次,其实是没有必要的. 如果你每次都像这样的方式调用的 ...

  9. Python基础学习总结__Day1

    一.Python是一门什么类型语言 1.解释型:一边编译一边执行,劣势是运行速度慢,但通过运用PyPy交互解释器(JIT技术)会让python程序执行速度快很多.优势是可移植性强. 2.强类型:即类型 ...

  10. Python周末21天笔记

    模块一: 基础相互据类型之间的相互转换 1. 字符串str 与 列表 list 与字典 dict 以及 元祖tuple的转换 例一: 把字典的key和value的值取出来,按照顺序存入到list中 d ...