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. html css 前端基础 学习方法及经验分享

    前言: 入园第一天,想分享一点儿前端基础html css 的学习方法和一些经验给热爱前端,但是不知道从哪儿开始,依旧有些迷茫的新手朋友们.当然,适合每个人的学习方式不同,以下所讲的仅供参考. 一.关于 ...

  2. LINUX - awk命令之NF和$NF区别 (转)

    NF和$NF 区别问答:(转)1.awk中$NF是什么意思?#pwd/usr/local/etc~# echo $PWD | awk -F/ '{print $NF}'etcNF代表:浏览记录的域的个 ...

  3. shell脚本之流程控制

      shell脚本之流程控制 shell脚本之流程控制 条件语句 条件判断 循环语句for,while,until for循环 while循环 until循环 循环控制语句continue 循环控制语 ...

  4. SSH复用代码最终版

    web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...

  5. 转 Java输入输出流详解(非常详尽)

    转  http://blog.csdn.net/zsw12013/article/details/6534619 通过数据流.序列化和文件系统提供系统输入和输出. Java把这些不同来源和目标的数据都 ...

  6. elasticsearch 中文分词、插件的安装和使用(一)

    1. 安装elasticsearch.kibana.x-pack #安装elasticsearch wget https://artifacts.elastic.co/downloads/elasti ...

  7. 机器视觉----LBP

    最近一直在做多视图的聚类与分裂,想要图片有更多的视图,就得对图片的特征进行抽取,那我们来聊聊图片的LBP特征. Local binary patterns (局部二值模式),是机器视觉中重要的一种特征 ...

  8. 在 docker 容器中捕获信号

    我们可能都使用过 docker stop 命令来停止正在运行的容器,有时可能会使用 docker kill 命令强行关闭容器或者把某个信号传递给容器中的进程.这些操作的本质都是通过从主机向容器发送信号 ...

  9. Spring 具名参数NamedParameterJdbcTemplate

    具名参数: 具名参数:SQL 按名称(以冒号开头)而不是按位置进行指定. 具名参数更易于维护, 也提升了可读性. 具名参数由框架类在运行时用占位符取代 我们之前一直是用JDBCTemplate  进行 ...

  10. ApplicationContextAware

    1.实现了ApplicationContextAware接口,在Bean的实例化时会自动调用setApplicationContext()方法! 2.通过调用静态方法getBean即可获取 sprin ...