LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description
Link: https://leetcode.com/problems/majority-element/description/
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 exists in the array.
给定一个size为n的数组,找出众数。其中众数出现了大于 ⌊ n/2 ⌋次。
2. 思路Thoughts
这题比较有意思的是,有一个优于hash map的算法 - 由Robert S. Boyer和J Strother Moore发表的MJRTY - A Fast Majority Vote Algorithm。
这个算法有一个限制就是这个众数必须要出现大于 ⌊ n/2 ⌋次。
由可以看到 wikipedia 伪代码可以写成:
- Initialize an element m and a counter i with i = 0
- For each element x of the input sequence:
- If i = 0, then assign m = x and i = 1
- else if m = x, then assign i = i + 1
- else assign i = i − 1
- Return m
怎么去思考这个问题呢?思路是这样的:
假设我有AAAAABBCD,很明显A是我们要找的目标。假设A出现了$x$次,非A的字母出现了$y$次,那么我们肯定有 $x > $ ⌊ n/2 ⌋ 而且$x>y$必须成立。
这也就意为着,如果我们删去其中任何两个不一样的字母$c_1$和$c_2$,而且这两个字母符合$c_1 \ne c_2$,那么:
a) 如果这两个字母包含A,例如删去A和C,那么$x = x - 1$, $y = y-1$, $x>y$依然成立因为$x-1>y-1 \ if \ x>y$。
b) 如果这两个字母都不包含A,例如删去B和D,那么$x$不变,$y=y-2$,$x>y$依然成立因为$x>y-2 \ if \ x > y$
所以如果删去任何两个不一样的字母,并不影响最后的结果。删完了不一样的,留下的一定会是那个目标众数。
3. 代码Code
class Solution {
public int majorityElement(int[] nums) {
int count = 1, current = nums[0];
for(int i = 1; i < nums.length; i++) {
if(count == 0) {
count++;
current = nums[i];
}
else if(current==nums[i])
count++;
else
count--;
}
return current;
}
}
LeetCode 169. Majority Element - majority vote algorithm (Java)的更多相关文章
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- Majority Element,Majority Element II
一:Majority Element Given an array of size n, find the majority element. The majority element is the ...
- leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java [Leetcode 169]Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that ...
- 169 Majority Element [LeetCode Java实现]
题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- [HTML]在页面中输出空格的几种方式
JS中如何输出空格 在写JS代码的时候,大家可以会发现这样现象: document.write(" 1 2 3 "); 结果: 1 2 ...
- 关于spring配置文件的头部编写
//普通spring配置文件模板1 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns: ...
- hdu Hat's Fibonacci(用了kuangbin模板)
大数的位数设置很坑,设成700会越界,设成800会超空间,最后设成了750居然就过了.... #include <iostream> #include <cstdio> #in ...
- 洛谷P4383 [八省联考2018]林克卡特树lct(DP凸优化/wqs二分)
题目描述 小L 最近沉迷于塞尔达传说:荒野之息(The Legend of Zelda: Breath of The Wild)无法自拔,他尤其喜欢游戏中的迷你挑战. 游戏中有一个叫做“LCT” 的挑 ...
- 用Python代码实现微信跳一跳作弊器
最近随着微信版本的更新,在进入界面有个跳一跳的小游戏,在网上看到技术篇教你用Python来玩微信跳一跳 ( 转载自 " 工科给事中的技术博客 " ) 本文旨在总结,技术全靠大神完成 ...
- [转]Javascript removeChild()删除节点及删除子节点的方法(同样适用于jq)
Javascript removeChild()删除节点及删除子节点的方法 这篇文章主要介绍了Javascript removeChild()删除节点及删除子节点的方法的相关资料,需要的朋友可以参考下 ...
- python学习笔记:第7天 深浅拷贝
目录 1. 基础数据类型补充 2. set集合 3. 深浅拷贝 1. 基础数据类型补充 (1)join方法 join方法是把一个列表中的数据进行拼接,拼接成字符串(与split方法相反,split方法 ...
- MCUXpresso release build 时提示GFLIB等函数未引用的问题
MCUXpresso release build 时提示 GFLIB 等函数未引用的问题 最近在使用 MCUXpresso 编译工程时选择 Debug(Debug build) 能顺利编译,但是选择 ...
- Random类与Random方法
class Test{ public static void main(String[] args){ int min=2; //定义随机数的最小值 int max=102; //定义随机数的最大值 ...
- 6 生成器 yield 协程
1.生成器 ----> 1 b = [x*2 for x in range(100000000000)] MemoryError: 想生成一个存放很多数据的列表,但是又不想内存占用太多 每次用一 ...