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 ...
随机推荐
- HDBS之应用代码优化
一.目录结构树 总体概述 代码检测工具sonar HDBS代码优化 总结开发注意点 二.总体概述 进入现在这家公司我的第一个任务就是对HDBS进行代码质量优化.HDBS可能大家不是很了解,现在给大家简 ...
- c#随便聊聊数据库操作
最近在学习web后台以及Python,到了程序员的转折年纪了,哎.估计很久不会写博文了.言归正传. 在原理的数据库连接池HiKari项目上.我扩展了独立的3个库,说是3个库,其实原本该是一个库.先聊聊 ...
- My collage goals
PART ONE: THE GOALS OF GRADE ONE 1, Try my best to improve my GPA , keep it around 4.0 2, Learn mor ...
- 简单的HTTP协议
HTTP 协议和 TCP/IP 协议族内的其他众多的协议相同,用于客户端和服务器之间的通信. 请求访问文本或图像等资源的一端称为客户端,提供资源响应的一端称为服务器端. 在两台计算机之间使用 HTTP ...
- JavaScript脚本加载相关知识
<script>标签的位置 HTML4规范允许<script>可以放在<head>或<body>中. 但是,放在<head>中会导致性能问题 ...
- mysql 优化(索引)
表 collect 字段 id(int 自增),title(varchar),info(text),vtype(int) 表中数据130w: select * from collect whe ...
- IO流之字符流
字符流产生的原因: 1.每次只能够读取一个字节或者一个字节数组,每次在需要转换成字符或者字符串的时候不是很方便2.不同的操作系统针对换行符的处理不方便3.有的时候会出现中文乱码(中文占两个字节,如果针 ...
- python新手第一天学习笔记-第一个ptyhon程序和python变量
一.python 的注释和第一个python 程序 : 1.单行注释 # Author Xiajq 2.多行注释 ''' ------------注释内容----------------------- ...
- Leecode刷题之旅-C语言/python-141环形链表
/* * @lc app=leetcode.cn id=141 lang=c * * [141] 环形链表 * * https://leetcode-cn.com/problems/linked-li ...
- Lambda表达式的语法与如何使用Lambda表达式
Lambda表达式是对象,是一个函数式接口的实例 如何来写Lambda表达式? 看参数 看返回值 代码实例1: package day2; import jdk.nashorn.internal.co ...