分析过程:在这个题目中,有一个计算机科学中很普遍的思想,就是如何把一个问题转化为规模较小的若干问题。分治、递归和贪心等都有这样的思想。在转化过程中,如果能保证小的问题跟原问题的解决是一致的就成功了。这样,我们可以通过寻找这样的方式将小问题转化为更小的问题。这需要我们将大问题拆成小问题,将大规模的数据降成小规模的数据,而不影响解。

问题解法
如果每次删除不同的数,不管删除的数是否包含众数,在剩下的数中,众数出现的次数仍然超过总数的一半,可以通过不断重复这个过程,将列表中的数的总数降低,从而得到问题的答案。总的时间复杂度为o(n),且只需要常数的额外内存。
数组中有个数字出现的次数超过了数组长度的一半。也就是说,有个数字出现的次数比其他所有数字出现的次数的和还要多。因此我们可以考虑在遍历数组的时候保存两个值,一个是数组中的一个数字,一个是次数。当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数增加1。如果下一个数字和我们之前保存的数字不同,则次数减1。如果次数为0,我们就保存下一个数字,并把次数重新设为1。

实现代码

int majorityElement(vector<int>& nums) {int times=0;int canadite;int length = nums.size();for (int i = 0; i < length; i++) {if (times == 0) {canadite = nums[i];times++;}else {if (nums[i] == canadite) {times++;}else {times--;}}}return canadite;}

159.majority element相关推荐

  1. Majority Element:主元素

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

  2. leetcode 169. Majority Element

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

  3. LeetCode Majority Element

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

  4. [LeetCode] Majority Element

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

  5. 【LeetCode】169. Majority Element

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

  6. Majority Element

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

  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. 【LeetCode 169】Majority Element

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

最新文章

  1. 多伦多到温莎_我想要freeCodeCamp Toronto的Twitter来发布报价,所以我做了一个免费的bot来做到这一点。...
  2. 【杂谈】Oracle里scott的由来
  3. 绝了!华为、H3C、锐捷交换机配置命令大全
  4. Shell多线程实现
  5. JavaScript 打印结果时多出一个undefined/出现undefined
  6. Android-消息机制总结
  7. 如何使用libxml2库[转]
  8. mysql事务最大个数_事务、mysql数据库的默认最大连接数、分页
  9. c语言的返回类型是指针变量吗,C语言-指针类型
  10. 开启mysql远程连接
  11. 17. PHP 表单处理
  12. 浅谈移动前端的最佳实践
  13. Java后台开发知识一览
  14. 2020线性代数辅导讲义练习答案
  15. 简易抽奖软件逻辑实现
  16. 论文学习 AInnoFace:Accurate Face Detection for High Performance
  17. 公司企业常见部门名称英译(zt)
  18. 用python画枫叶代码-Python自定义函数基础
  19. Comet OJ CCPC-Wannafly Winter Camp Day8 A Aqours
  20. 我的专业计算机作文700字,我的电脑作文700字

热门文章

  1. 实习成长之路: MySQL三 : 事务隔离:为什么你改了我还看不见?
  2. python 老师和父亲_父亲节丨有个当老师的爸爸是怎样的体验
  3. 作为程序员的我,接私活有错么?
  4. 怎么避免后台被搜索_优化亚马逊后台关键词的6个技巧,让买家快速找到你
  5. combox控件触发事件_Simulink(三角函数和代数约束模块)+Matlabgui(Gui实例)+Stateflow数据与事件(三)...
  6. java socket 简书_Java之Socket编程入门篇
  7. 前牙正常覆盖是多少_深覆合千万不要矫正?用图示告诉你深覆合深覆盖的区别是什么,有什么危害...
  8. python中‘configparser’的使用方法---ini文件读取方法
  9. Python 多线程基本步骤
  10. 帆软图表折线图Y轴更改为ABCD等级标签折线的显示改造