当前位置:网站首页>【刷题篇】多数元素(超级水王问题)
【刷题篇】多数元素(超级水王问题)
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;
}
边栏推荐
- 第十届中国云计算大会·中国站:展望未来十年科技走向
- Reflection and planning of a sophomore majoring in electronic information engineering
- Captura下载安装及在Captura配置FFmpeg
- How does the pytorch project run?
- MongoDB簡介
- sigaction的使用
- [learning notes] seckill - seckill project - (11) project summary
- MongoDB简介
- Introduction à mongodb
- User value is the last word in the competition of mobile phone market
猜你喜欢
pytorch项目怎么跑?
[embedded module] OLED display module
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
Mongodb replication set [master-slave replication]
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
js中#号的作用
ffmpeg之 一张/多张图片合成视频
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
Message queue addition failure
Ffmpeg recording screen and screenshot
随机推荐
Web session management security issues
What is pytorch? Is pytorch a software?
Using jasmine to monitor constructors - spying on a constructor using Jasmine
基于Pytorch和RDKit的QSAR模型建立脚本
Role of JS No
[MySQL] the difference between left join, right join and join
[Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
For instruction, uploading pictures and display effect optimization of simple wechat applet development
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
小程序获取用户头像和昵称
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
[Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
redis高级应用【密码防护、数据持久化、主从同步、哨兵模式、事务】【暂未完成(半成品)】
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
可分离债券与可转债
FileZilla Client下載安裝
Table structure of Navicat export database
Download and install captura and configure ffmpeg in captura
shardingsphere动态数据源