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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

【思路1】java HashMap

我们用java的HashMap,把数组中的值做为键,把元素的数目作为值。如果出现了一个元素的数目大于nums.length/2,则返回该值。代码如下:

 public class Solution {
public int majorityElement(int[] nums) {
if(nums.length==0 || nums==null) return 0;
Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums.length; i++){
if(map.containsKey(nums[i])){
if(map.get(nums[i])+1 > nums.length/2) return nums[i];
map.put(nums[i], map.get(nums[i])+1);
}
else
map.put(nums[i], 1);
}
return nums[0];
}
}

【思路2】

由于数组中肯定存在一个majority number。因此我们可以遍历数组,如果找到一对值不同的元素就把他们删除,那么最后剩下的一定majority number。这种算法叫Moore’s Voting Algorithm,由Robert S.Boyer 和J Strother Moore于1980年发明,是线性时间复杂度。

举个例子:[1,2,3,1,1,1] (1,2)是一对不相同的元素,(3,1)是另一对不相同的元素,那么剩下的1肯定是majority number。代码如下:

 public class Solution {
public int majorityElement(int[] nums) {
if(nums.length==0 || nums==null) return 0;
int count = 0;
int result = 0; for(int i = 0; i < nums.length; i++){
if(count == 0){
result = nums[i];
count = 1;
}
else{
if(nums[i] == result){
count ++;
}
else count --;
}
}
return result;
}
}

当然,这种算法对于存在主元素的数组是有效的,如: A A A C C B B C C C B C C

它肯定能返回主元素C。但是,如果不存在主元素,那么得到的结果就跟遍历顺序有关了。如: A A A C C C B

如果是从左到右,那么结果是B,如果是从右到左,那么结果是A。

LeetCode OJ 169. Majority Element的更多相关文章

  1. 【一天一道LeetCode】#169. Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. LeetCode Problem 169: Majority Element查找多数元素

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

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

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

  4. LeetCode【169. Majority Element】

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

  5. 【LeetCode】169 - Majority Element

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

  6. LeetCode OJ 229. Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  7. 【LeetCode OJ】Majority Element

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

  8. LeetCode OJ:Majority Element(主要元素)

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

  9. LeetCode OJ:Majority Element II(主元素II)

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. 面向对象---final关键字

    final 在java中表示的意思是最终,也称为完结器,可以用final关键字声明类.属性.方法,在声明时需要注意以下几点: 1.使用final声明的类不能有子类. 2.使用final声明的方法不能被 ...

  2. Java 不使用科学计数法表示数据设置

    java.text.NumberFormat nf = java.text.NumberFormat.getInstance(); nf.setGroupingUsed(false); nf.form ...

  3. 在虚拟机上配置linux lab的相关经验

    最近一直在研究怎样在嵌入式开发板上移植linux嵌入式系统,但是不太想花费太多钱购买开发板.然后在网上搜索相关的arm模拟器.有qemu,skyeye,armulator等,在按照网上教程一步一步实践 ...

  4. 自定义实现IEnumerable

    Demo: http://files.cnblogs.com/files/georgeHeaven/Demo.IEnumerable.rar 一.使用场景 在开发过程中,经常需要使用foreach来循 ...

  5. Ddos 攻击

    title: Ddos 防御相关 tags: Ddos, 安全, 防御 grammar_cjkRuby: true --- 防御基础 1.1. 攻击流量到底多大 谈到DDoS防御,首先就是要知道到底遭 ...

  6. Chapter 21_2 模式匹配函数

    基础函数比较简单,就是几个普通的函数string.byte.string.char.string.rep.string.sub.string.format还有大小写转换函数upper和lower. 接 ...

  7. vga显示彩条

    vga显示驱动程序可分为扫描行列和行列同步两个部分 //注意:只有在有效区域内给vga赋值才会有颜色变化 assign vga_b = isready ? vga_s[:] :'d0; assign ...

  8. centos-mysql 安装

    初学者自编文档,如有错误,请指出,具体命令就不阐述了,不明白 度娘吧! nginx我是编译安装在服务器上 和其他安装应该会有区别 安装路径路径:/usr/local/ 安装包存放位置:/home/ap ...

  9. html绑定

    目的 html绑定可以绑定DOM元素内的HTML内容. 示例: <div data-bind="html: details"></div> <scri ...

  10. python基础-安装篇

    1.安装之前我们要先去python的官网下载python的安装包 下载地址:https://www.python.org/downloads/ Python 官网有两个版本一个是3.5.2(最新版)一 ...