当前位置:网站首页>[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;
}
边栏推荐
- pytorch是什么?pytorch是一个软件吗?
- 2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
- Null and undefined
- TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
- 递归使用和多维数组对象变一维数组对象
- How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
- In Net 6 project using startup cs
- shardingsphere动态数据源
- 2022 polymerization process examination questions and polymerization process examination skills
- Bisher - based on SSM pet adoption center
猜你喜欢

Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet

Docker install and start MySQL service

node,npm以及yarn下载安装

Recursion: one dimensional linked lists and arrays

Error in compiled file: error: unmapped character encoding GBK

Appium automated testing framework

pytorch项目怎么跑?

JS native common knowledge

What can learning pytorch do?

Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
随机推荐
中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
pytorch项目怎么跑?
ffmpeg录制屏幕和截屏
nodejs基础:浅聊url和querystring模块
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
【学习笔记】seckill-秒杀项目--(11)项目总结
没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥
Error in compiled file: error: unmapped character encoding GBK
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Idea shortcut keys
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
Applet get user avatar and nickname
Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
Mongodb replication set [master-slave replication]
golang xxx. Go code template
For instruction, uploading pictures and display effect optimization of simple wechat applet development
Download and install captura and configure ffmpeg in captura
[Yu Yue education] reference materials of political communication science of Communication University of China
Interface embedded in golang struct