题意理解

求一个序列的众数,出现次数大于n / 2的数。

问题分析

数组

Boyer-Moore 投票算法

遍历每个元素,count连续出现的数字个数,candiate表示数字本身。如果count为0,当前数字作为候选,如果当前数字不等于候选count-1,否则count+1.

其他

复杂度是o(n), 空间复杂度是o(1)

链接

    int majorityElement(vector<int>& nums) {int count = 0;int candidate = 0;for(int a : nums)    //遍历序列{if (count == 0)    //当前计数为0{candidate = a;    //候选变成当前数}count += (candidate == a ? 1 : -1);    //候选等于当前数,计数加1,不等于计数就减1.}return candidate;    //最后的候选就是求的众数。}

https://leetcode.com/articles/majority-element/

169. Majority Element【力扣】相关推荐

  1. leetcode讲解--169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  2. [Easy] 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

  4. leetcode python3 简单题169. Majority Element

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十九题 (1)题目 英文: Given an array of size n ...

  5. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. 【LeetCode】169. Majority Element

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

  7. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【LeetCode从零单排】No.169 Majority Element(hashmap用法)

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  9. [LeetCode]: 169: Majority Element

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  10. 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

最新文章

  1. [导入]实时数据库的经典书
  2. controller是什么意思_光谈理论有什么意思,附源码带大家实操SpringBoot
  3. java对象转为json字符串
  4. c#初学-索引器get和set的使用(泛型类)
  5. Oracle排序中NULL值处理的五种常用方法
  6. PyQt5 技术篇 - Qt Designer怎么用styleSheet设置按钮的背景
  7. 9-Qt6 QString和QChar
  8. 通过 Socket 实现 TCP 编程入门
  9. 对外汉语语料库有哪些_史上最全最新的语料库资源大全【对外汉语教学研究工具】...
  10. CDH集群清理磁盘空间
  11. GitHub加速访问、克隆和下载
  12. 如何用基于保利威视云平台来做网络年会直播活动
  13. 辅材先生之打开传统向互联网+辅材这把锁
  14. 黑苹果11.0big sur驱动因特尔英特尔intel82599万兆网卡教程
  15. 有声语音计算机软件,有声语音计算器
  16. 永磁同步电机力矩控制(二):FOC与DTC
  17. Samba之打印机共享配置nbsp;(CUPS…
  18. html css 布局_创建有吸引力HTML CSS动漫主题网站布局
  19. 开源!MatrixBench:实时物联网场景的数据压测“兵法秘籍”
  20. Kali Linux渗透测试 093 自动注入-Sqlmap

热门文章

  1. 【笔记-vue】《imooc-vue.js高仿饿了么》、《imooc-vue 音乐app》、《imooc-vue.js源码全方位解析》
  2. Centos7.5服务器安装HDP的坑
  3. 2019移动广告反欺诈算法挑战赛baseline
  4. 吃饭,,睡觉,,打豆豆,,
  5. Fabric安装中make release无法编译问题解决 no required module provides package
  6. Linux入门真经-032文件系统的创建与管理
  7. java 代码实现日历表
  8. 营业执照变更法人需要准备什么资料
  9. 用 Vue+ElementUI 搭建后台管理极简模板
  10. Web迅雷带来的...