【Lintcode】046.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.
Notice
You may assume that the array is non-empty and the majority number always exist in the array.
Given [1, 1, 1, 1, 2, 2, 2], return 1
题解:
摩尔投票法
Solution 1 ()
class Solution {
public:
int majorityNumber(vector<int> nums) {
int cnt = ;
int res = nums[];
for(auto n : nums) {
if (n == res) {
cnt++;
continue;
} else {
if(--cnt <= ) {
cnt = ;
res = n;
}
}
}
return res;
}
};
【Lintcode】046.Majority Number的更多相关文章
- 【转】oracle数据库NUMBER数据类型
原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a) precision表示数字中的有效位;如果没有指定prec ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【BZOJ4026】dC Loves Number Theory 分解质因数+主席树
[BZOJ4026]dC Loves Number Theory Description dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源. 给 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【POJ2699】The Maximum Number of Strong Kings(网络流)
Description A tournament can be represented by a complete graph in which each vertex denotes a playe ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
随机推荐
- 写一段代码,判断一个包括'{','[','(',')',']','}'的表达式是否合法(注意看样例的合法规则。) 给定一个表达式A,请返回一个bool值,代表它是否合法。
这道题比较奇怪,它的匹配规则并不是我们平时想想的那种匹配规则,例如:平时的匹配规则是().{}.[]才能匹配,本题中(和} .].)都能匹配.所以做题时要好好审题.另外,本题中给的测试用例是错误的. ...
- onvif 开发之video streamer---onvif实现功能和经验
目录(?)[-] 一产生onvif源码框架 从wsdl生成C头文件 从头文件生成源码框架 二创建soap运行环境 三RTSP视频对接 实现GetCapabilities命令 实现GetServices ...
- LeetCode -- Flatten 二叉树
这个题目主要考察二叉树的先序遍历. 1. 先序遍历2. 节点用队列存储3. 遍历队列,建立链表 实现: public class Solution { public void Flatten(Tree ...
- Process Stats:了解你的APP怎样使用内存
原文地址:http://android-developers.blogspot.com/2014/01/process-stats-understanding-how-your.html?m=1 原作 ...
- JS 之 数据类型转换
首先我们来简单了解一下JS中的数据类型,JavaScript拥有字符串.数字.布尔.数组.对象.Null.Undefiend 6中数据类型.同一时候,JavaScript拥有动态类型. 也 ...
- iOS程序自动检测更新的实现
本文转载至 http://blog.csdn.net/davidsph/article/details/8931718 App Store自动更新itunes 之前项目需要用到app自动更 ...
- GS踢玩家下线功能
GS踢玩家下线功能 //key:userId, val:nChannelId (当前在线用户) std::map<int, int> m_mapOnLineUserByUid; ///&l ...
- 九度OJ 1178:复数集合 (插入排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8393 解决:1551 题目描述: 一个复数(x+iy)集合,两种操作作用在该集合上: 1.Pop 表示读出集合中复数模值最大的那个复数,如 ...
- Python爬虫--Urllib库
Urllib库 Urllib是python内置的HTTP请求库,包括以下模块:urllib.request (请求模块).urllib.error( 异常处理模块).urllib.parse (url ...
- 关于Wix的源代码
Wix的源代码有两种方式可以获得,以3.8为例: 在Release的页面下载wix38-debug.zip 通过SourceCode页面下载,http://wix.codeplex.com/Sourc ...