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.
代码: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 实现的更多相关文章
- LeetCode Majority Element Python
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 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 ...
随机推荐
- PHP线程安全和非线程安全有什么区别
我们先来看一段PHP官网的原话: Which version do I choose? IIS If you are using PHP as FastCGI with IIS you should ...
- YII2 定义页面提示
控制器里面这样写: 单条消息: 键值是规定好的,不要去自定义哦! \Yii::$app->getSession()->setFlash('error', 'This is the mess ...
- linux 命令——12 more (转)
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...
- 【BZOJ1013】[JSOI2008] 球形空间产生器(高斯消元)
点此看题面 大致题意: 给定一个\(n\)维球体上的\(n+1\)个点,请你求出这个球体的圆心的位置. 列出方程 这一看就是一道解方程题. 我们可以设这个球体的圆心的位置为\((x_1,x_2,..x ...
- python_62_装饰器5
import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time ...
- Drupal相关网站推荐
http://nodeone.se/ drupal7安装过程中,如果选择简体中文,到导入翻译时出现错误,原因是执行超时了. 方法一: 修改php.ini文件:memory_limit = 256M ( ...
- 模板引擎原理及underscore.js使用
为什么要使用模板引擎 DOM结构简单,完全可以使用DOM方法创建DOM树.$("<td></td").appendTo(); 当页面比较复杂的时候,下面的程序中红 ...
- eclipse部署web项目至本地的tomcat但在webapps中找不到问题
一.发现问题 在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并 ...
- 黑马基础阶段测试题:创建一个存储字符串的集合list,向list中添加以下字符串:”C++”、”Java”、” Python”、”大数据与云计算”。遍历集合,将长度小于5的字符串从集合中删除,删除成功后,打印集合中的所有元素
package com.swift; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; ...
- js数据结构与算法--递归
递归,函数自己调用自己 return 返回值, 后面的代码不执行 function fn(num){ console.log(num) if(num == 0){ return; } fn(num-1 ...