题意理解

求一个序列的众数,出现次数大于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. 2019年python课本_2019年Python学习指南
  2. day04_07-三个函数的区别
  3. Java 6.编写类
  4. 23种设计模式及其对应实例-转
  5. Linux学习之CentOS(八)--Linux系统的分区概念
  6. oracle 最大值及其_学习笔记:Oracle优化 SQL查询最大值 最小值时的优化方法案例...
  7. 老实人一般容易吃亏,但是老实人遇到的机会比别人多一点点!
  8. 8051、ARM、AVR
  9. win2003 sp2 设置NLB
  10. poj3984(经典dfs)
  11. c 转易语言源码,易语言代码转HTML 测试(源码方式)
  12. AD16操作技巧笔记
  13. chrome谷歌插件
  14. 阿里大鱼短信接口PHP版,精简版阿里大鱼短信SMS发送接口PHP实例
  15. Microsoft Visual SourceSafe 2005 简体中文版
  16. Springboot定时任务【多线程处理】
  17. 人体神经系统示意图手绘,人体神经结构图示意图
  18. 微信小程序:十二星座运势查询
  19. Field baseMapper in com.xxx required a single bean,but 100 were found --------Mybatis-Plus
  20. 华为鸿蒙系统深圳,鸿蒙操作系统面世 华为称“把不可能变为可能”

热门文章

  1. 文件及文件夹设置密码
  2. Android官网培训课:用ViewPager做屏幕幻灯片
  3. Umlet和draw.io 使用心得
  4. python安装出现modify_Win10系统提示“Python 0x80070643安装时发生严重错误”怎么办?...
  5. 哔哩哔哩网站前端源码_分享一个仿制哔哩哔哩镜子网站源码
  6. 黑科技 —— OTG 线
  7. qq如何好友满了删除好友后还能收到好友消息?
  8. sql注入 学习笔记
  9. win2012磁盘分区
  10. 最小系统开发板——利用STM32F103C8T6实现LED流水灯