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. Android 动画学习笔记

    Android动画的两种:Frame帧动画.Tween动画(位移动画)[实现:存放目录res/anim] Tween动画:(位移.缩放.旋转):通过对场景里的对象不断做图像变换. 四种效果Alpha. ...

  2. WPF非UI线程获取修改控件属性值的方法

    public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, ...

  3. Sql中判断"库、表、列,视图,存储过程"是否存在

    --判断数据库是否存在 IF EXISTS (SELECT * FROM MASTER.sys.sysdatabases WHERE NAME = '库名') PRINT 'exists ' else ...

  4. ASP.NET 跨域获取JSON天气数据

    前几天做一个门户网站,在首页需要加载气象数据,采用了中央气象局的接口. 刚开始采用JSONP在前台跨域请求数据,没成功~ 后换成在c#后台请求数据返回... 前端代码: $(function () { ...

  5. AMQ学习笔记 - 20. 使用Apache ActiveMQBrowser监控ActiveMQ

    概述 Apache ActiveMQBrowser可以用于查看AMQ中的消息.这里对其使用方法进行简单介绍. 使用介绍 1.下载并解压缩 下载地址:Apache ActiveMQBrowser,当前最 ...

  6. Codevs 1010 过河卒

     时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同 ...

  7. Microsoft.Xna.Framework.TitleContainer.OpenStream()

    /// <summary> /// This method opens a file using System.IO classes and the /// TitleLocation p ...

  8. php关联不上mysql解决办法

      ## php无法关联mysql  php关联不上mysql解决办法: 尝试N多方法,需要的dll文件都复制了,依旧是不断提示: Fatal error: Call to undefined fun ...

  9. SQL 面试题(一)

    问题来自于CSDN问答,练练SQL吧. 测试数据SQL代码: if OBJECT_ID('td_ls_2') is not null drop table td_ls_2 go if OBJECT_I ...

  10. C#多线程与UI响应 跨线程更新UI

    最近在写一个TCP通信程序,自定义了一个通信类TCPclient,用于客户端异步接收和发送网络消息. TCPclient中定义了一个接收到新的网络消息事件: //收到新消息事件 public dele ...