当前位置:网站首页>[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
- Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
- Read a paper_ ChineseBert
- Table structure of Navicat export database
- 简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
- Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
- 2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
- 在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
- nodejs基础:浅聊url和querystring模块
- pytorch项目怎么跑?
猜你喜欢

Error in compiled file: error: unmapped character encoding GBK

Interaction free shell programming

FileZilla Client下載安裝

IPv6 transition technology-6to4 manual tunnel configuration experiment -- Kuige of Shangwen network

JS native common knowledge

Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot

Recursion: depth first search

Cnopendata China Customs Statistics

Role of JS No

没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
随机推荐
leetcode:297. 二叉树的序列化与反序列化
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
【刷题篇】多数元素(超级水王问题)
Docker install and start MySQL service
eth入门之DAPP
eth入门之简介
[mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
Separable bonds and convertible bonds
redis在服务器linux下的启动的相关命令(安装和配置)
Ffmpeg download and installation tutorial and introduction
IPv6 foundation construction experiment
C语言HashTable/HashSet库汇总
Applet (continuous update)
Reflection and planning of a sophomore majoring in electronic information engineering
Commands related to the startup of redis under Linux server (installation and configuration)
Makefile demo
递归使用和多维数组对象变一维数组对象
JMeter starts from zero (III) -- simple use of regular expressions
Cnopendata China Customs Statistics
node,npm以及yarn下载安装