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. 基于canvas的仪表盘效果

    概述 基于Canvas实现的仪表盘及效果.通过配置参数,可以任意修改仪表盘颜色,刻度,动画过渡时间等,满足不同场景下的使用.同时使用原生的Canvas,也是学习Canvas的很好的例子. 详细 代码下 ...

  2. ASP.NET总结

    ASP.NET已经学习完.学牛腩的时候面对一些控件和方法会用,但对当中的原理还不懂.学习这部分的内容时, 从头到尾都有一种相识的感觉,把之前一些不懂得地方也理解了,每个知识都有相应的样例练习,学起来还 ...

  3. javax.naming.NoInitialContextException: Need to specify class name in environment or system property

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property ...

  4. Hadoop集群+Spark集群搭建(一篇文章就够了)

    本文档环境基于ubuntu16.04版本,(转发请注明出处:http://www.cnblogs.com/zhangyongli2011/ 如发现有错,请留言,谢谢) 一.准备 1.1 软件版本 Ub ...

  5. 【LeetCode】76. Minimum Window Substring

    Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...

  6. C# 基础: new 和 overrider 的区别

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. linux下利用shell脚本和mysqldump热备份和恢复mysql

    对mifeng数据库进行每周六3:33完全热备份,并可以完全恢复! 一.先建立备份脚本 #vi /mifengbackup/backup.sh #!bin/bash cd /mifengbackup ...

  8. Java字节码指令

    1. 简介 Java虚拟机的指令由一个字节长度的.代表着某种特定操作含义的数字(称为操作码)以及跟随其后的零至多个代表此操作所需参数(称为操作数)而构成. 由于Java虚拟机采用面向操作数栈而不是寄存 ...

  9. 微软七届MVP桂素伟:移动互联网与职业规划

    原文地址:http://student.csdn.net/mcd/topic/163587/955481 2014年10月19日在哈尔滨工业大学举办了CSDN高校俱乐部全国巡讲. 此次邀请到了微软七届 ...

  10. 【C语言】19-static和extern关键字1-对函数的作用

    一.extern与函数 在前面我提到过一句话:如果一个程序中有多个源文件(.c),编译成功会生成对应的多个目标文件(.obj),这些目标文件还不能单独运行,因为这些目标文件之间可能会有关联,比如a.o ...