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)的更多相关文章

  1. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  2. Majority Element,Majority Element II

    一:Majority Element Given an array of size n, find the majority element. The majority element is the ...

  3. 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 ...

  4. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

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

  5. Java for LeetCode 169 Majority Element

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

  6. Java [Leetcode 169]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  7. 169 Majority Element [LeetCode Java实现]

    题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...

  8. [LeetCode] 169. Majority Element 多数元素

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

  9. leetcode 169 Majority Element 冰山查询

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

随机推荐

  1. Oracle split分区表引起ORA-01502错误

    继上次删除分区表的分区遇到ORA-01502错误后[详细见链接:Oracle分区表删除分区引发错误ORA-01502: 索引或这类索引的分区处于不可用状态],最近在split分区的时候又遇到了这个问题 ...

  2. 19-3-6Python中字典的解释、使用、嵌套

    一.字典 为什么学字典: 列表的缺点: 1.列表如果存储的数据比较多,那么他的查询速度相对慢. 2.列表存储的数据关联性不强. 字典是什么: Python基础数据类型之一:字典. Python中唯一的 ...

  3. 常用模块 - shutil模块

    一.简介 shutil – Utility functions for copying and archiving files and directory trees.(用于复制和存档文件和目录树的实 ...

  4. Spring Boot 2.0 设置网站默认首页

    Spring Boot设置默认首页,方法实验OK如下 附上Application启动代码 /** * @ClassName Application * @Description Spring-Boot ...

  5. python 视频配音、剪辑

    一.FFmpeg的使用 首先下载FFmpeg然后将FFmpeg添加到环境路径中.运行cmd 输入ffmpeg无报错表示成功. 二.python中的使用 在python中执行cmd命令需要调用subpr ...

  6. mybatis调用存过程返回结果集和out参数值

    Mapper文件: 1.配置一个参数映射集,采用hashMap对象 2.使用call调用存储过,其中in out等标识的参数需要有详细的描述,例如:mode,JavaType,jdbcType等 &l ...

  7. Mac进度条卡在100%

  8. Python中常见的字典dict处理

    #字典的赋值d = [{"dasda": 123, "gsgsg": 3344}, {"dasdz": 123, "gsksg&q ...

  9. BGP扩展属性

    安全特性: BGP安全特性: MD5 GTSM:通用TTL安全保护机制通过对TTL的检测来达到防止攻击的目的,如果攻击者模拟真实的BGP协议报文(TCP),对一台路由器不断地发送报文,路由器接收报文后 ...

  10. golang 协程嵌套,会产生依赖关系(父子关系)么?

    编码时冒出一个问题:在一个协程内部,再创建一个或多个协程,是否会产生依赖关系? 做了一个小实验,这里随笔记录一下经过,备注后续深入研究. test代码: package main import ( & ...