【Lintcode】046.Majority Number
2024-09-07 04:26:39
题目:
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.
Have you met this question in a real interview?
Yes
Example
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;
}
};
最新文章
- VB.NET 如何进行调用HTTP外部接口
- Java Web 学习链接
- android小知识之fragment中调用startActivityForResult(Intent intent,int requestcode)所遇到的问题
- 配置Windows 2008 R2 64位 Odoo 8.0 源码PyCharm开发调试环境
- ActionResult
- iOS开发——开发必备OC篇&;UITableView设置界面完整封装(四)
- Android listview的item设定高度
- POJ 2243 Knight Moves
- UITouch触摸事件
- 学生管理系统(list)
- 灵活运用Zend框架
- Session和Cookie的使用总结
- python服务端多进程压测工具
- java----dom4j 解析XML
- JQ删除数组中的某个对象
- 脚本解决.NET MVC按钮重复提交问题
- LeetCode算法题-Remove Linked List Elements(Java实现)
- 手动设定统计数据 set_table_stats
- MVC中的HtmlHelper详解
- P1006 传纸条-洛谷luogu-dp动态规划