题目:

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 = 0;int res = nums[0];for(auto n : nums) {if (n == res) {cnt++;continue;} else {if(--cnt <= 0) {cnt = 1;res = n;}}}return res;}
};

转载于:https://www.cnblogs.com/Atanisi/p/6910262.html

【Lintcode】046.Majority Number相关推荐

  1. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  2. mysql的number类型对应的db2_【转】oracle数据库NUMBER数据类型

    NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定precision的话,Oracle将使用38作为精度. b)  如果scale大于零,表 ...

  3. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  4. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  5. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

  6. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

  7. 【Lintcode】1647. Path Search

    题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...

  8. 【Lintcode】1723. Shortest Path in a Grid with Obstacles Elimination

    题目地址: https://www.lintcode.com/problem/shortest-path-in-a-grid-with-obstacles-elimination/descriptio ...

  9. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

最新文章

  1. 为啥led灯用一年后暗了很多_有些人日间车灯可以关为啥偏偏不关?是要告诉别人他的车好吗?...
  2. [译] Security Token 生态 — 项目指南
  3. QRadioButton Toggled() 使用方法
  4. android studio3.12,Android Studio V3.12环境下TV开发教程(六)提供卡片视图
  5. 51单片机的定时器深入讲解
  6. linux 继续编译,【编译】Linux环境编译traceroute
  7. c# 可变二维数组_C#| 具有固定行大小和可变列大小的二维数组
  8. 【托管服务qin】WEB网站压力测试教程详解
  9. 个人的博客搭建(持续更新)
  10. wifidog java_wifidog 配置中文说明
  11. html 手机楼层布局,纯html、css、javascript实现楼层跳跃式的页面布局
  12. 织梦charset.func.php,织梦程序百度php主动推送代码,亲测可用!
  13. iOS开发之UIApplication
  14. centos7 python3 爬虫登陆邮箱_Centos7搭建Scrapy爬虫环境
  15. 诺顿企业版安装找不到路径
  16. Conda 下 安装 Allennlp
  17. d3中元素拖拽drag实例
  18. SQL Server解决18456错误方案
  19. oracle stdevp函数,ORACLE 系统函数与SQLSERVER系统函数的区别是什么
  20. java 类加载器的理解及加载机制?

热门文章

  1. 播放视频android学习笔记---44_在线视频播放器,网络视频解析器,SurfaceView 控件使用方法...
  2. PostgreSQL操作问题(转载)
  3. SQL Server中EXISTS结构
  4. 关键词热度分析工具_亚马逊卖家: 产品关键词挖掘分析必备工具!
  5. python壁纸程序代码_python设置windows桌面壁纸的实现代码
  6. 为什么硬盘速度忽快忽慢_C盘装软件会拖慢电脑速度?C盘是不是比其他盘快?...
  7. php vue jwt 实战,Vue路由之JWT身份认证的实现方法
  8. oracle11中过程continue,Oracle 11g OCM备考之创建EM与EM登陆异常的处理
  9. 计算机应用12班,《计算机应用基础》上机试卷A(10级机电8—12班)
  10. hiv实验室抗体筛查流程图_HIV自测都有哪些方法?结果准确么?