题目:

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;
}
};

最新文章

  1. VB.NET 如何进行调用HTTP外部接口
  2. Java Web 学习链接
  3. android小知识之fragment中调用startActivityForResult(Intent intent,int requestcode)所遇到的问题
  4. 配置Windows 2008 R2 64位 Odoo 8.0 源码PyCharm开发调试环境
  5. ActionResult
  6. iOS开发——开发必备OC篇&amp;UITableView设置界面完整封装(四)
  7. Android listview的item设定高度
  8. POJ 2243 Knight Moves
  9. UITouch触摸事件
  10. 学生管理系统(list)
  11. 灵活运用Zend框架
  12. Session和Cookie的使用总结
  13. python服务端多进程压测工具
  14. java----dom4j 解析XML
  15. JQ删除数组中的某个对象
  16. 脚本解决.NET MVC按钮重复提交问题
  17. LeetCode算法题-Remove Linked List Elements(Java实现)
  18. 手动设定统计数据 set_table_stats
  19. MVC中的HtmlHelper详解
  20. P1006 传纸条-洛谷luogu-dp动态规划

热门文章

  1. 自己定义ProgressDialog载入图片
  2. wifi认证Portal开发系列(四):portal协议的java封装
  3. python学习(六)元组学习
  4. SQL Server 的collate的含义
  5. 允许局域网内其他主机访问本地MySql数据库
  6. 目标检测之vibe---ViBe(Visual Background extractor)背景建模或前景检测
  7. SERVICE_STATUS结构各成员解析
  8. 修改zend studio字符集
  9. 视频服务之ffmpeg部署
  10. JAVA解析XML之DOM方式