当前位置:网站首页>【刷题篇】多数元素(超级水王问题)
【刷题篇】多数元素(超级水王问题)
2022-07-03 03:52:00 【m0_60631323】
一、题目
OJ链接
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
要求:时间复杂度O(N) 空间复杂度O(1)
二、题解
2.1思路
核心思路:
详细流程:
2.2源码:
public int majorityElement(int[] arr) {
int cand=0;
int HP=0;
for (int i = 0; i < arr.length; i++) {
if(HP==0){
cand=arr[i];
HP=1;
}else if(cand==arr[i]){
HP++;
}else {
HP--;
}
}
return cand;
}
边栏推荐
- 递归:一维链表和数组
- Ansible introduction [unfinished (semi-finished products)]
- Debug: CD cannot be used in kaggle
- Makefile demo
- 学会pytorch能干什么?
- Captura下载安装及在Captura配置FFmpeg
- Download and install node, NPM and yarn
- Error c2694 "void logger:: log (nvinfer1:: ilogger:: severity, const char *)": rewrite the restrictive exception specification of virtual functions than base class virtual member functions
- Leetcode: dynamic planning template
- Ffmpeg recording screen and screenshot
猜你喜欢

Docker install and start MySQL service
![Mongodb replication set [master-slave replication]](/img/2c/8030548455f45fa252062dd90e7b8b.png)
Mongodb replication set [master-slave replication]

NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
![[MySQL] the difference between left join, right join and join](/img/d4/8684cd59cd1bd77e70bd4d7c7074c3.jpg)
[MySQL] the difference between left join, right join and join

简易版 微信小程序开发之for指令、上传图片及展示效果优化

How to download pytorch? Where can I download pytorch?

pytorch是什么?pytorch是一个软件吗?
![[embedded module] OLED display module](/img/c4/474f5ee580d132654fbd1a4cd53bab.jpg)
[embedded module] OLED display module

Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences

TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
随机推荐
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
递归:一维链表和数组
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
2022 tea master (primary) examination questions and tea master (primary) examination question bank
[embedded module] OLED display module
Web session management security issues
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
[set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
阿洛对自己的思考
Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
[combinatorics] basic counting principle (addition principle | multiplication principle)
Mysql Mac版下载安装教程
CEPH Shangwen network xUP Nange that releases the power of data
Null and undefined
Pytorch multi card distributed training distributeddataparallel usage
[mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
Role of JS No