当前位置:网站首页>leetcode-169.多数元素
leetcode-169.多数元素
2022-07-26 03:14:00 【KGundam】
数学问题
题目详情
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
示例1:
输入:nums = [3,2,3]
输出:3
示例2:
输入:nums = [2,2,1,1,1,2,2]
输出:2
思路:
题目中一个重要的点是给定的数组一定存在多数元素,即在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素
那么这道题就可以用摩尔投票法解决了,摩尔投票法就是遍历数组,先将第一个数设置为"候选人" candidate,然后将票数cnt’初始化为1(只有他自己的一票),然后往后面遍历,如果遇到相同数,就加票,不同数则减票,如果票数减到0了,就更换下一个候选人,继续遍历(注意后面的候选人可能会和前面候选人重复,所以不必回头遍历)
再借用评论区一位大神对摩尔投票法的形象描述:
摩尔投票法:
核心就是对拼消耗。
玩一个诸侯争霸的游戏,假设你方人口超过总人口一半以上,并且能保证每个人口出去干仗都能一对一同归于尽。最后还有人活下来的国家就是胜利。
那就大混战呗,最差所有人都联合起来对付你(对应你每次选择作为计数器的数都是众数),或者其他国家也会相互攻击(会选择其他数作为计数器的数),但是只要你们不要内斗,最后肯定你赢。
最后能剩下的必定是自己人.
我的代码:
class Solution
{
public:
int majorityElement(vector<int>& nums)
{
int n = nums.size(), candidate = nums[0], cnt = 1;
//因为初始化candidate为nums[0]所以i要从1开始遍历了
for (int i = 1; i < n; ++i)
{
if (candidate == nums[i]) //如果遇到相同的数
{
++cnt; //就"加一票"
}
else //遇到不同的数
{
--cnt; //就"减一票"
if (cnt == 0) //减完票如果没票了
{
//说明这个候选人的票绝对不是大于n/2的(即不为多数元素)
candidate = nums[i]; //那就更换下一个候选人
cnt = 1; //初始票再次为1
}
}
}
return candidate;
}
};
边栏推荐
- NFT因无意义而美丽
- Service gateway (zuul)
- els 回调函数、退出消息
- Win11 hide input method status bar method
- ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
- canvas——绘制文本——饼图的修改
- 【数学建模-规划模型总结】| MATLAB求解
- There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first
- [Yuri crack man] brings you easy understanding - deep copy and shallow copy
- Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting
猜你喜欢

离线数据仓库从0到1-阶段二软件安装

在混合云中管理数据库:八个关键注意事项

Alibaba Sentinel - cluster traffic control

What are you interviewing for in a big factory? It's worth watching (I)

PXE efficient batch network installation

How to install with USB flash disk?

Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
![[noip2001 popularization group] packing problem](/img/b7/1310b3e68d0ee016465fc069315af6.png)
[noip2001 popularization group] packing problem

Opencv 在图像上进行标注(画框+写字)

easyExcel设置行隐藏,解决setHidden(true)失效问题
随机推荐
LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS
Where can Lora and nb-iot be used
canvas——心电图的设计,以及如何清理画布
Installation and operation of orb-slam2 under ROS
TCP experimental verification
Dominate the salary list! What industry has a "money" path?
ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
els 初始化窗口类
[NOIP2001 普及组] 最大公约数和最小公倍数问题
Intensive reading of the paper -yolov1:you only look once:unified, real time object detection
在混合云中管理数据库:八个关键注意事项
Use eventlog analyzer for log forensics analysis
QT notes - Q_ Q and Q_ D learning
QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
CMD CPM command summary
Canvas - ECG design and how to clean the canvas
YOLOv3: An Incremental Improvement
canvas——绘制图片——动图制作
Golang log programming system