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 ...
随机推荐
- Java通过链表实现队列
class LinkedQueue<T> { /** * 队列大小,由构造函数初始化 */ private int maxSize; /** * 队头 */ private Node fr ...
- eclipse设置背景保护色及常用设置
1.设置背景颜色 2.代码自动补全 Windows-->Preferences-->Java-->Editor-->Content Asist,在Auto activation ...
- Jmeter之性能测试基础
1.概念:性能测试是通过自动化的测试工具模拟多种正常峰值及负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行.通过负载测试,确定在各种工作负载下系统的性能,目标 ...
- testTenuringThreshold()方法的分析与问题处理
代码如下: public class TestTenuringThreshold { private static final int _1MB = 1024 * 1024; /** * vm-arg ...
- mapreduce自定义排序(map端1.4步)
3 3 3 2 3 1 2 2 2 1 1 1 -----------------期望输出 1 1 2 1 2 2 3 1 3 2 3 3 将以上数据进行排序,排序规则是:按照第一列升序排序,如果第一 ...
- ui-router
学习历程:1 ng-router --> 2 location --> 3 $location --> 4 promise --> 5 html5 history -- ...
- MMORPG战斗系统随笔(二)、浅谈场寻路Flow Field PathFinding算法
转载请标明出处http://www.cnblogs.com/zblade/ 今天给大家带来一篇游戏中寻路算法的博客.去年,我加入一款RTS的游戏项目,负责开发其中的战斗系统,战斗系统的相关知识,属于游 ...
- bzoj2118(加法原理)(墨墨的等式)
题目大意:给定n个物品,每个物品有一个非负价值,问[L,R]区间内有多少价值可以被凑出来. 题意网上一大片,具体求解过程是利用了加法原理,将各个模数拥有的个数之和相加. 就是说随机取一个数a[k],那 ...
- Java中的类型擦除与桥方法
类型擦除 Java在语法中虽然存在泛型的概念,但是在虚拟机中却没有泛型的概念,虚拟机中所有的类型都是普通类.无论何时定义一个泛型类型,编译后类型会被都被自动转换成一个相应的原始类型. 比如这个类 pu ...
- 聊聊Java中几种常用的设计模式
1.单例模式(有的书上说叫单态模式其实都一样) 该模式主要目的是使内存中保持1个对象.看下面的例子: package org.sp.singleton; //方法一 public class Sing ...