当前位置:网站首页>[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;
}
边栏推荐
猜你喜欢
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
Recursion: quick sort, merge sort and heap sort
Mongodb installation & Deployment
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
Mysql Mac版下载安装教程
How does the pytorch project run?
FileZilla client download and installation
2022 P cylinder filling examination content and P cylinder filling practice examination video
在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
随机推荐
[embedded module] OLED display module
FileZilla Client下载安装
Idea shortcut keys
[MySQL] the difference between left join, right join and join
递归:快速排序,归并排序和堆排序
2022 polymerization process examination questions and polymerization process examination skills
Half of 2022 is over, so we must hurry up
QSAR model establishment script based on pytoch and rdkit
leetcode:297. 二叉树的序列化与反序列化
【DRM】DRM bridge驱动调用流程简单分析
User value is the last word in the competition of mobile phone market
【学习笔记】seckill-秒杀项目--(11)项目总结
释放数据力量的Ceph-尚文网络xUP楠哥
IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
Intercept string fixed length to array
Mongodb replication set [master-slave replication]
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi