Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.

Example

For [1, 1, 1, 1, 2, 2, 2], return 1

Challenge

O(n) time and O(1) space

Solution:

 public class Solution {
/**
* @param nums: a list of integers
* @return: find a majority number
*/
public int majorityNumber(ArrayList<Integer> nums) {
if (nums==null || nums.size()==0) return -1;
int len = nums.size(); int cur = nums.get(0);
int count = 1;
for (int i=1;i<len;i++){
if (cur!=nums.get(i)){
count--;
if (count==0){
cur = nums.get(i);
count=1;
}
} else {
count++;
}
} return cur;
}
}

LintCode-Majority Number的更多相关文章

  1. Lintcode: Majority Number III

    Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...

  2. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  3. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

  4. [LintCode] Majority Number 求众数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  5. Lintcode: Majority Number II

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...

  6. LintCode Majority Number II / III

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...

  7. [LintCode] Majority Number 求大多数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  8. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...

  9. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  10. 【Lintcode】046.Majority Number

    题目: Given an array of integers, the majority number is the number that occurs more than half of the ...

随机推荐

  1. .net C# 网页播放器 支持多种格式 媒体播放器 播放器 代码

    .avi格式代码片断如下:<object id='video' width='400' height='200' border='0' classid='clsid:CFCDAA03-8BE4- ...

  2. SqlServer知识总结

    SqlServer查询表的列数 select count(*) from sysobjects a join syscolumns b on a.id=b.id where a.name='表名' 在 ...

  3. 给一个Entity的字段付初始化值(C#)

    给一个类去分别赋值,是一个很繁琐切无趣的工作. 那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式.   具体的想法就是,利用类的属性,取出所有 ...

  4. 2015英特尔® 实感™ (Intel® RealSense™) 动手开发实验课

    2015年英特尔® 全球实感技术动手实验课路演来到中国, 这次在中国将有北京和广州两站,包括一天的动手实验室活动 - 面向对感知计算.3D 开发和虚拟现实兴趣浓厚的开发人员.英特尔专家将会指导您如何借 ...

  5. sql with as用法详解

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让 ...

  6. GForms展现服务云开发平台

    GForms完全基于开放标准,使用XForms作为面向服务的架构简单易用的前端,帮助用户跨多个行业加速数据整合.GForms提供可视化设计器,实现展现服务开发中数据与模型完全分离,加快开发速度快速投入 ...

  7. [GeekBand]C++高级编程技术(2)

    本篇笔记主要分为两个主要部分,第一部分关于对象模型,第二部分是关于new和delete的更加深入的学习. 一.对象模型 关于vptr(虚指针)和vtbl(虚函数表) 只要用到了虚函数,对象中就会多一个 ...

  8. 关于CORS

    前几天碰到CORS问题,只要在“Access-Control-Allow-Origin”响应头中添加对应域名即可. 今天做一个上传文件的demo,利用XMLHttpRequest向服务器发送post请 ...

  9. System Generator入门

      System generator 安装之后会在Simulin模块库中添加一些Xilinx FPGA专用的模块库,包括Basic Element,Communication,Control Logi ...

  10. 【转】AOP知识点

    ref:http://www.diybloghome.com/prology/975.html 一.概念理解 老规矩,还是先看官方解释:AOP(Aspect-Oriented Programming, ...