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 ...
随机推荐
- 在phalcon框架下,php接口规范以及接口实例
接口规范实例 前言 由于本人也是第一次写接口,之前对于接口也是一知半解,没有系统的了解过,所以这次也是写的自己的在这几天在APP项目中关于接口的浅层次的认识,如果有不妥或者不当的地方还请指出,再此谢谢 ...
- 全新jquery多点滑动幻灯片——全屏动画animateSlide
首页banner的酷炫效果多来自全屏大图的幻灯片动画,下面提供一种完美兼容的jquery动画特效:全新jquery多点滑动幻灯片——全屏动画animateSlide(代码完全原创). 直接上代码,把h ...
- jQuery滚动时动态加载页面内容
var loading = false; $(window).scroll(function(){ if((($(window).scrollTop()+$(window).height())+250 ...
- 每天一道LeetCode--389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- SQL Server 2008 报表服务入门
目录 报表服务的安装与配置 开发报表的过程 报表制作实例 一.报表服务的安装与配置 1. 报表服务的组件 2. 报表服务 2008 的安装 2.1在SQL Server 2008上安装报表服务有两种方 ...
- DataGridView 操作
//dataGridView 删除选中行 int num = dataGridView2.SelectedRows.Count; ) { DataGridViewRow r = dataGridVie ...
- OS中常用的调度算法总结 (转)
http://blog.chinaunix.net/uid-25132162-id-361291.html 一.常见的批处理作业调度算法 1.先来先服务调度算法(FCFS):就是按照各个作业进入系统的 ...
- Window下配置NodeJs环境详解
今年打算学习Web这块,所以就买了本Node.js+MongoDb+AngularJS这本书,这周天也比较忙,想着录视频(拍小片,不是AV,不要误会,是在线课程)的事情,这周又将Asp.Net ...
- button等按钮onclientclick事件失效
如果确定JS没写错 第一种方法: 在JS方法最后return false; 调用方法前加上return 第二种方法: 在JS方法最后event.returnValue=false; 附加:event. ...
- 20140122-Application19事件