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. Python 学习笔记(十一)Python语句(三)

    While 循环语句 用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务. 语法: while 判断条件: 执行语句…… 执行语句可以是单个语句或语句块.判断条件可以是任何 ...

  2. 二、HDFS 架构

    源自:http://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html HDFS has a m ...

  3. AJAX上传文件到服务器

    上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的, Html部分 <input type="file& ...

  4. Oracle 触发器(一)

    1)触发器是一种特殊的存储过程,触发器一般由事件触发并且不能接受参数,存储器由语句块去调用:触发器是当某个事件发生时自动地隐式运行. 2)触发器分类: 1.DML触发器: 创建在表上,由DML事件引发 ...

  5. phpStudy环境安装SSL证书教程(apache)

    https://cloud.tencent.com/product/ssl 此链接是检测域名 证书的可以检测一下 下面是证书配置 小白呢亲测 作为PHP程序员,我们一定要学会使用phpStudy环境集 ...

  6. 网站程序application, session, cookie区别的一点小总结

    cookie:是存放于本地的文件,可以设置期限,一般来讲是比较小文本或键值对等,主要用于记录用户浏览信息,账号密码等等.可以让人们的登录变得方便,因为有了cookie,在一段时间内就可以自动登录以前所 ...

  7. 面试:Hbase和Hive的区别

    区别: 1. Hive是一个构建在Hadoop基础设施之上的数据仓库,通过HQL查询存放在HDFS上的数据,不能交互查询.HBase是一种Key/Value系统,它运行在HDFS之上,可以交互查询. ...

  8. “子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析

    为了实现下述代码,首先得有数据库和相应的表格,本文用的是https://blog.csdn.net/qaz13177_58_/article/details/5575711/中的案例,即先用链接中那些 ...

  9. 第5天 Java基础语法

    第5天 Java基础语法 今日内容介绍 方法 方法 方法概述 在我们的日常生活中,方法可以理解为要做某件事情,而采取的解决办法. 如:小明同学在路边准备坐车来学校学习.这就面临着一件事情(坐车到学校这 ...

  10. LeetCode初级算法的Python实现--数组

    LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...