当前位置:网站首页>[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] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
- Ffmpeg one / more pictures synthetic video
- IPv6 foundation construction experiment
- In Net 6 project using startup cs
- User value is the last word in the competition of mobile phone market
- 2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
- Table structure of Navicat export database
- pytorch项目怎么跑?
- Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
- 动态规划:最长回文子串和子序列
猜你喜欢

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

How to download pytorch? Where can I download pytorch?
![[embedded module] OLED display module](/img/c4/474f5ee580d132654fbd1a4cd53bab.jpg)
[embedded module] OLED display module

Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange

递归:深度优先搜索

Mongodb installation & Deployment

js中#号的作用
![Ansible introduction [unfinished (semi-finished products)]](/img/2a/0003daf761ba02d8837c4657fc3f29.png)
Ansible introduction [unfinished (semi-finished products)]

2022 tea master (primary) examination questions and tea master (primary) examination question bank

Recursion: quick sort, merge sort and heap sort
随机推荐
在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
pytorch是什么?pytorch是一个软件吗?
Ffmpeg one / more pictures synthetic video
释放数据力量的Ceph-尚文网络xUP楠哥
C语言HashTable/HashSet库汇总
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
[MySQL] the difference between left join, right join and join
[DRM] simple analysis of DRM bridge driver call process
PHP generates PDF tcpdf
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
Bisher - based on SSM pet adoption center
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
[learning notes] seckill - seckill project - (11) project summary
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
Applet (continuous update)
Open Visual Studio 2010 hangs when opening a SQL file sql file
pytorch项目怎么跑?
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
For instruction, uploading pictures and display effect optimization of simple wechat applet development