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.

public class Solution {
public int majorityElement(int[] num) {
// assuming the num is not null and empty. int most = num[0];
int counter = 1; for(int i=1; i<num.length; i++) {
if(num[i] == most) {
++counter;
} else {
if(counter==0) {
most = num[i];
++counter;
} else {
--counter;
}
}
}
return most;
}
}

leetcode 153: Majority Element的更多相关文章

  1. [LeetCode] 169. Majority Element 多数元素

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

  2. [LeetCode] 229. Majority Element II 多数元素 II

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

  3. LeetCode 169. Majority Element (众数)

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

  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 、229. Majority Element II

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

  6. LeetCode 169. Majority Element - majority vote algorithm (Java)

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

  7. 【leetcode】Majority Element

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

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

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

  9. LeetCode 169. Majority Element

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

随机推荐

  1. Eclipse默认编码格式设置方式

    看图即可 STEP ONE: STEP TWO: STEP THREE: STEP FOUR: 项目右击——>Properties 参阅: eclipse编码格式设置 - AlanLee(Jav ...

  2. LaTeX去掉默认显示日期时间

    LaTeX去掉默认显示日期时间: \date{}

  3. STS(Spring Tool Suite)创建maven项目

    右键菜单选择新建->maven项目 自己创建存放配置文件需要使用的maven文件夹

  4. REST开放接口生成文档工具之apidoc

    一.安装node.js环境 感谢阿里云,下载的链接http://npm.taobao.org/mirrors/node/latest-v6.x/ 二.安装apidoc npm install apid ...

  5. unity, 如果碰撞使用2d物理,为防止颤动,需将更新position的代码写在FixedUpdate里

    例如我为主角添加了一个circle collider 2d,和一个rigidbody 2d,为障碍物添加了一个circle collider 2d. 然后我在主角的Update函数里更新位置让主角由A ...

  6. Android从无知到有知——NO.5

    今天整一下利用广播实现ip拨号. 这一块主要用到的知识是android四大组件之中的一个的broadcast   receiver(广播接收者).那么它接收什么东东呢,就是我们所无谓的一个个的事件,比 ...

  7. /^(0|[1-9]\d*)([.]5)?$/ 在PHP正则中是什么意思 ?

    ^以什么开头 ()分组 |或的意思 \d 匹配任何数字字符串 [-] |[-]\d* 或1-9之间的数+任意数字零次或多次 开头 ()分组 []原子表 [.]5匹配. ? 零次或1次 总结: 必须以0 ...

  8. swift 函数.和匿名函数

    函数 注意: 没有定义返回类型的函数会返回特殊的值,叫 Void.它其实是一个空的元组(tuple),没有任何元素,可以写成(). 使用元组作为返回参数,返回多个参数 func count(strin ...

  9. 结构体内重载小于号< 及构造函数

    struct Node { int d, e; bool operator < (const Node x) const { return x.d < d; } Node(int d, i ...

  10. Delphi之Raise抛出异常

    相关资料: http://blog.csdn.net/a20071426/article/details/10160171 实例代码: unit Unit1; interface uses Windo ...