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 ...
随机推荐
- 【作业留存】根据IATF框架,设计的一种中小型企业安全拓扑
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- hive 报错FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient FAILED: Execu
使用hive一段时间以后,今天在使用的时候突然报错,如下: hive> show databases;FAILED: Error in metadata: java.lang.RuntimeEx ...
- php使用GD库实现图片水印和缩略图——给图片添加文字水印
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- pycharm tab换为4个空格
Edit => find => replace 然后勾上 Regex,上一行输入 \t,下一行输入4个空格.
- js在一个div里面移动其子div
var ChildDiv = $("#cid"); var width = 0; //鼠标点击子div的地方和子div的左边边距距离 var height = 0; //鼠标点击子 ...
- [Rails学习之路]Rails文件结构与路由
约定优于配置和RESTful是Ruby on Rails十分推崇的哲学.在一个默认的RESTful的Rails项目中,使用资源和HTTP动词来帮助组织项目. 假如有一个使用scaffold创建的Rai ...
- C#中?和??用法
在C#中“?”有三种用法. 1.可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空,例如:string str=null;是正确的.int i= ...
- 【BZOJ2427】[HAOI2010] 软件安装(缩点+树形DP)
点此看题面 大致题意: 有\(N\)个软件,每个软件有至多一个依赖以及一个所占空间大小\(W_i\),只有当一个软件的直接依赖和所有的间接依赖都安装了,它才能正常工作并造成\(V_i\)的价值.求在容 ...
- Videos
Videos 时间限制: 1 Sec 内存限制: 128 MB提交: 17 解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 C-bacteria takes charg ...