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..
Example
Given [1, 1, 1, 1, 2, 2, 2], return 1
Challenge
O(n) time and O(1) extra space
.
链接地址
http://www.lintcode.com/en/problem/majority-number/
解法
int majorityNumber(vector<int> nums) {
// write your code here
int ret, num = 0;
for (int i = 0; i < nums.size(); i++) {
if (num == 0) {
ret = nums[i];
num++;
} else {
if (nums[i] == ret) {
num++;
} else {
num--;
}
}
}
return ret;
}
算法解释
一个思路。同一时候删除两个不同的数。那么结果不会变
Majority Number的更多相关文章
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Majority Number I & || && |||
Majority Number Given an array of integers, the majority number is the number that occurs more than ...
- 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
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
- 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 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 II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
随机推荐
- dataTable组件使用
dataTable组件使用:引入JS $("#id").DataTable({ scrollY:450, //开始滚动高度 lengthChange:false , // ...
- 遍历Request.QueryString
Request.QueryString 返回的是 NameValueCollection, 而NameValueCollection实现了IEnumerable的GetEnumerator方法,只是G ...
- 一个程序员一月的开销统计分析、(附上PC端和移动端android源码)
图片1(类别): 图片中的数据是我一月的花费统计分析. 这是该网站的地址:http://www.10086bank.com/(需要登录) 没有帐号马上注册一个吧.进入系统后点击“记一笔” 添加数据 ...
- [POJ 1005] I Think I Need a Houseboat C++解题
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 81874 ...
- 静态方法中不能使用 $this
忽略了一个问题,$this 代表当前对象!! 静态方法中应该使用 类名 . self 或者 static 关键字来代替! static public function get_info($id) { ...
- Diango路由控制
路由的格式: #路由配置的格式: urls.py里面写 from diango.conf.urls import url urlpatterns = [ url(正则表达式,views视图函数,nam ...
- HDU——1003Max Sum(子序列最大和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树
同BZOJ3779. SDOI出原题,还是弱化版的. 吃枣药丸 #include <map> #include <cmath> #include <queue> # ...
- Miracast HDCP 等知识
Miracast 通讯架构中关于视频数据处理流程的部分.整个视频数据处理及传输的流程,大致上分为几个阶段,一开始将撷取到系统的画面及声音进行压缩,而压缩后的影音数据再转为基本封包串流(Packetiz ...
- Pebbles(hdu 2167)
题意:给定一个N*N的方格,让你在里面取出一些数使其和最大,要求每一个数不能与其相邻的8个数同时取出. /* 和hdu1565差不多,只不过在更新的时候考虑斜上方的格子对该格子的影响. */ #inc ...