当前位置:网站首页>[brush questions] most elements (super water king problem)
[brush questions] most elements (super water king problem)
2022-07-03 03:55:00 【m0_ sixty million six hundred and thirty-one thousand three hun】
One 、 subject
OJ link
Given a size of n Array of nums , Return most of the elements . Most elements refer to the number of occurrences in an array Greater than ⌊ n/2 ⌋ The elements of .
You can assume that the array is not empty , And there are always many elements in a given array .
requirement : Time complexity O(N) Spatial complexity O(1)
Two 、 Answer key
2.1 Ideas
The core idea :
Detailed process :
2.2 Source code :
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;
}
边栏推荐
- [mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
- Docker install and start MySQL service
- C language hashtable/hashset library summary
- Applet (continuous update)
- 【DRM】DRM bridge驱动调用流程简单分析
- ffmpeg下载安装教程及介绍
- 如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
- Shardingsphere dynamic data source
- Role of JS No
- Arduino application development - LCD display GIF dynamic diagram
猜你喜欢

Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis

nodejs基础:浅聊url和querystring模块

Ffmpeg one / more pictures synthetic video

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

Bisher - based on SSM pet adoption center

ffmpeg录制屏幕和截屏

leetcode:动态规划模板

105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode

2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video

Recursion: quick sort, merge sort and heap sort
随机推荐
Mutex and rwmutex in golang
2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank
释放数据力量的Ceph-尚文网络xUP楠哥
学会pytorch能干什么?
第十届中国云计算大会·中国站:展望未来十年科技走向
redis在服务器linux下的启动的相关命令(安装和配置)
pytorch开源吗?
Applet get user avatar and nickname
PHP generates PDF tcpdf
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
pytorch是什么?pytorch是一个软件吗?
JS native common knowledge
Arlo's thinking about himself
C language hashtable/hashset library summary
Mongodb replication set [master-slave replication]
Idea shortcut keys
基于Pytorch和RDKit的QSAR模型建立脚本
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
【DRM】DRM bridge驱动调用流程简单分析
2022 tea master (primary) examination questions and tea master (primary) examination question bank