159.majority element
分析过程:在这个题目中,有一个计算机科学中很普遍的思想,就是如何把一个问题转化为规模较小的若干问题。分治、递归和贪心等都有这样的思想。在转化过程中,如果能保证小的问题跟原问题的解决是一致的就成功了。这样,我们可以通过寻找这样的方式将小问题转化为更小的问题。这需要我们将大问题拆成小问题,将大规模的数据降成小规模的数据,而不影响解。
问题解法:
如果每次删除不同的数,不管删除的数是否包含众数,在剩下的数中,众数出现的次数仍然超过总数的一半,可以通过不断重复这个过程,将列表中的数的总数降低,从而得到问题的答案。总的时间复杂度为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相关推荐
- Majority Element:主元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode】169. Majority Element
原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...
- Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode - 169. Majority Element (多数投票问题)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode从零单排】No.169 Majority Element(hashmap用法)
题目 Given an array of size n, find the majority element. The majority element is the element that app ...
- [LeetCode]: 169: Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
最新文章
- 多伦多到温莎_我想要freeCodeCamp Toronto的Twitter来发布报价,所以我做了一个免费的bot来做到这一点。...
- 【杂谈】Oracle里scott的由来
- 绝了!华为、H3C、锐捷交换机配置命令大全
- Shell多线程实现
- JavaScript 打印结果时多出一个undefined/出现undefined
- Android-消息机制总结
- 如何使用libxml2库[转]
- mysql事务最大个数_事务、mysql数据库的默认最大连接数、分页
- c语言的返回类型是指针变量吗,C语言-指针类型
- 开启mysql远程连接
- 17. PHP 表单处理
- 浅谈移动前端的最佳实践
- Java后台开发知识一览
- 2020线性代数辅导讲义练习答案
- 简易抽奖软件逻辑实现
- 论文学习 AInnoFace:Accurate Face Detection for High Performance
- 公司企业常见部门名称英译(zt)
- 用python画枫叶代码-Python自定义函数基础
- Comet OJ CCPC-Wannafly Winter Camp Day8 A Aqours
- 我的专业计算机作文700字,我的电脑作文700字
热门文章
- 实习成长之路: MySQL三 : 事务隔离:为什么你改了我还看不见?
- python 老师和父亲_父亲节丨有个当老师的爸爸是怎样的体验
- 作为程序员的我,接私活有错么?
- 怎么避免后台被搜索_优化亚马逊后台关键词的6个技巧,让买家快速找到你
- combox控件触发事件_Simulink(三角函数和代数约束模块)+Matlabgui(Gui实例)+Stateflow数据与事件(三)...
- java socket 简书_Java之Socket编程入门篇
- 前牙正常覆盖是多少_深覆合千万不要矫正?用图示告诉你深覆合深覆盖的区别是什么,有什么危害...
- python中‘configparser’的使用方法---ini文件读取方法
- Python 多线程基本步骤
- 帆软图表折线图Y轴更改为ABCD等级标签折线的显示改造