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.


题目标签:Array

  忘记说了,特地回来补充,今天看完《中国有嘻哈》 复活赛,Jony J 回来了! 特激动! 一开始看的时候就喜欢他,虽然他忘词两次被淘汰!但是实力终究是在的,一挑五 荣耀回归!

  知道他消失好多集肯定是当不了冠军了! 但是呢,他回来可以让更多的人有机会听到他好听的歌,就足够了! Respect! 推荐给大家 《不用去猜》《套路》,写code 也要劳逸结合嘛,迷茫的时候听听,他的歌挺正能量的。

  

  题目给了我们一个array,里面必定会有一个众数,让我们找出众数。

  利用Moore Voting 摩尔投票法,设定一个count,和一个result,遍历nums array, 当count 等于0 的时候,把result 等于 当前的数字,更新count = 1;

  当遇到重复的数字时候,count++;

  当遇到不重复的数字时候,count--。

  因为众数的数量肯定大于nums array一半的数量,所以遍历完之后,不管它怎么++ --, 众数的数量肯定够其他的数字减,而且还有的剩,所以剩下的就是众数。

Java Solution:

Runtime beats 82.86%

完成日期:04/06/2017

关键词:Array

关键点:Moore Voting,众数的特性是数量 大于 总数的一半

 import java.util.Hashtable;
public class Solution
{
public int majorityElement(int[] nums)
{
// if there is only 1 or 2 numbers in array, nums[0] is the majority since there must have majority.
if(nums.length == 1 || nums.length == 2)
return nums[0]; int result = 0;
int count = 0;
// iterate the array
for(int i=0; i<nums.length; i++)
{
if(count == 0) // if count = 0, meaning start over from current one. The previous are cancel out.
{
result = nums[i];
count = 1;
}
else if(result == nums[i])
count++; // if the number is repeated, increase count.
else
count--; // if the number is not repeated, decrease count.
} // the leftover number must be the majority one since it appears more than half.
return result;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4233501.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 169. Majority Element (众数)的更多相关文章

  1. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  2. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  3. 23. leetcode 169. Majority Element

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

  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 - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  6. leetcode 169 Majority Element 冰山查询

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

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

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

  8. LeetCode 169. Majority Element

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

  9. Java for LeetCode 169 Majority Element

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

随机推荐

  1. 基于图形检测API(shape detection API)的人脸检测

    原文:https://paul.kinlan.me/face-detection/ 在 Google 开发者峰会中,谷歌成员 Miguel Casas-Sanchez 跟我说:"嘿 Paul ...

  2. Linux下的定时任务 - Cron服务

    最近搞咕自己的笔记系统,虽然现在是个人的使用,对于数据库的数据还是比较少,但是安全还是一个我必须注意的东西. (特别是前段时间中了比特币的病毒之后,更是让我关注了我的主机的安全的问题.) 今天的随记是 ...

  3. centOS 6启动流程

      centOS6启动流程 centOS6启动流程 linux内核组成 centos6启动大致流程 1.post加电自检 2.Boot Sequence 3.MBR引导 4.Grub启动 制作init ...

  4. SVN使用【介绍SVN、快速入门、解决冲突】

    什么是SVN SVN全称:Subversion,是一个开放源代码的版本控制系统 Svn是一种集中式文件版本管理系统.集中式代码管理的核心是服务器,所有开发者在开始新一天的工作之前必须从服务器获取代码, ...

  5. linux (1)基本知识/目录/磁盘格式/文件系统

    一.linux基本知识介绍1.命令行格式:(按两次tab可以知道有多少个可执行命令,我的有1980个,用户有1960个)[用户名@linux主机名 ~(当前目录)]$ 命令 选项 参数1 参数2[ro ...

  6. ResourceManager的HA

    大家都知道在hadoop2中对HDFS的改进很大,实现了NameNode的HA:也增加了ResourceManager.但是ResourceManager也可以实现HA.你没看错,确实是Resourc ...

  7. Eclipse 多行复制并且移动失效

    Eclipse  有个好用的快捷键  即 多行复制 并且移动 但是 用 Win7 的 电脑 的 时候 发现屏幕 在 旋转 解决方案: 打开Intel的显卡控制中心 把旋转  的 快捷键 进行更改 就好 ...

  8. Redis的安装以及在项目中使用Redis的一些总结和体会

    第一部分:为什么我的项目中要使用Redis 我知道有些地方没说到位,希望大神们提出来,我会吸取教训,大家共同进步! 注册时邮件激活的部分使用Redis 发送邮件时使用Redis的消息队列,减轻网站压力 ...

  9. 翻译连载 | 第 9 章:递归(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  10. ActiveMQ 入门helloworld

    1.下载安装ActiveMQ 官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Unix 等几个版本 ...