LeetCode 169. Majority Element (众数)
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 (众数)的更多相关文章
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 深入理解计算机系统(2.5)------C语言中的有符号数和无符号数以及扩展和截断数字
上一篇博客我们讲解了计算机中整数的表示,包括无符号编码和补码编码,以及它们之间的互相转换,个人觉得那是非常重要的知识要点.这篇博客我们将介绍C语言中的有符号数和无符号数以及扩展和截断数字. 1.C语言 ...
- [js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist
promise主要是用来解决异步回调问题,其实还有好几种比promise更好的方案,后面再说,这节,我们先用promise来改造下,我以前写的一篇文章[js高手之路]javascript腾讯面试题学习 ...
- 枚举类TimeUnit
枚举类TimeUnit 全路径为 java.util.concurrent.TimeUnit TimeUnit 主要用于通知基于时间的方法如何解释给定的计时参数 举例如下 如果 lock 不可用,则以 ...
- arm-linux-gcc 4.3.2编译uboot 1.1.6
在第三期项目的视频中,官方提供了一整套新的工具链,bootloader, 内核和文件系统(arm-linux-gcc_4.3.2, uboot-2012.04.01, linux-3.4.2)其中ub ...
- Tomcat配置用户名和密码
1.在浏览器输入http://localhost:8080/,打开Tomcat自带的默认主页面,右侧有"administration""documentation&quo ...
- ElasticSearch入门(2) —— 基础概念
在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,我们可以画一些简单的对比图来类比传统关系型数据库: Relational DB -> Dat ...
- 【小程序】微信小程序实现各种特效实例
写在前面 最近在负责一个微信小程序的前端以及前后端接口的对接的项目,整体上所有页面的布局我都已经搭建完成,里面有一些常用的特效,总结一下,希望对大家和我都能有所帮助 实例1:滚动tab选项卡 先看一下 ...
- activemq的安装与使用
一.activemq的安装 环境:CentOS 6.JDK8 1. 确保系统已安装了可用的jdk版本2. 从网上下载 Linux 版的 ActiveMQ( apache-activemq-5.11.1 ...
- cs224d 自然语言处理作业 problem set3 (一) 实现Recursive Nerual Net Work 递归神经网络
1.Recursive Nerual Networks能够更好地体现每个词与词之间语法上的联系这里我们选取的损失函数仍然是交叉熵函数 2.整个网络的结构如下图所示: 每个参数的更新时的梯队值如何计算, ...
- H5音频处理的一些小知识
前 言 LiuDaP 十一过后,小编要做一个关于音乐播放器的项目,要用到大量H5音频处理的内容,于是在十月一日国庆黄金周闲暇之际,自己学习了一下H5音频的相关内容.虽然自学的没有那么深入,但是对 ...