LintCode-Majority Number
Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.
For [1, 1, 1, 1, 2, 2, 2], return 1
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的更多相关文章
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- 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 ...
- [LintCode] Majority Number 求大多数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- 【Lintcode】046.Majority Number
题目: Given an array of integers, the majority number is the number that occurs more than half of the ...
随机推荐
- spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象
最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...
- 解决tsc.exe 已退出 代码为 1
最近从ABP上面下载一个项目,然后用VS2015编译后报错,在Web项目下ABP---Framework---scripts下面多了一个abp.d.ts,报了一大堆错误 上网一查,d.ts是TypeS ...
- IntelliJ IDEA 14.0.2破解注册码文件(2015年06月08日)
Java文件代码: import java.math.BigInteger;import java.util.Date;import java.util.Random;import java.util ...
- C#微信开发回复信息
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;usi ...
- sql 了解
char,varchar,nvarchar区别 类型 长度 使用说明 长度说明 char(n) 固定长度 索引效率高 程序里面使用trim去除多余的空白 n 必须是一个介于 1 和 8,000 之间 ...
- github 学习笔记【一】
这几天在学习github ,其实学了主要用来管理自己的项目!因为要在家里和公司两头做! 所以就开始学习使用!目前熟练几个命令,其他的一边用一遍学吧!想一举成功应该是不太可能的! 反复记忆才能更好,据说 ...
- jquery实现点击页面空白隐藏指定菜单
注意:dmenu是一个div的class名哦 代码如下 复制代码 $('html,body').click(function(e){ if(e.target.id.indexOf("dme ...
- ubuntu启动失败the system is running in low graphics mode
ubuntu启动失败the system is running in lowg raphics mode 起因 ubuntu重新设置selinux的模式 修改配置文件/etc/selinux/conf ...
- iOS 复杂tableView的 cell一般设计思路
- 初识 Jenkins
Jenkins: Jenkins 是一款获奖的跨平台持续集成和持续交付软件,可以大大提高生产力.Jenkins 用以构建和测试软件项目,帮助开发者更容易的实现项目变更的持续集成,帮助用户更容易的获取最 ...