当前位置:网站首页>【刷题篇】多数元素(超级水王问题)
【刷题篇】多数元素(超级水王问题)
2022-07-03 03:52:00 【m0_60631323】
一、题目
OJ链接
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
要求:时间复杂度O(N) 空间复杂度O(1)
二、题解
2.1思路
核心思路:
详细流程:
2.2源码:
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
- npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
- Filter
- numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
- Makefile demo
- Applet get user avatar and nickname
- Recursion: quick sort, merge sort and heap sort
- [set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
- Ffmpeg recording screen and screenshot
- How to download pytorch? Where can I download pytorch?
猜你喜欢

MongoDB簡介

Web session management security issues

Avec trois. JS fait une scène 3D simple

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

Ffmpeg download and installation tutorial and introduction

docker安装及启动mysql服务
![Mongodb replication set [master-slave replication]](/img/2c/8030548455f45fa252062dd90e7b8b.png)
Mongodb replication set [master-slave replication]

Recursion: one dimensional linked lists and arrays

SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode

Appium自动化测试框架
随机推荐
ffmpeg录制屏幕和截屏
User value is the last word in the competition of mobile phone market
[learning notes] seckill - seckill project - (11) project summary
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
Is pytorch open source?
Open Visual Studio 2010 hangs when opening a SQL file sql file
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
动态规划:最长回文子串和子序列
The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units
FileZilla Client下载安装
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Recursion: depth first search
Download and install node, NPM and yarn
[leetcode question brushing day 34] 540 Unique element in array, 384 Disrupt array, 202 Happy number, 149 Maximum number of points on a line
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
递归:一维链表和数组
Makefile demo
sigaction的使用
[embedded module] OLED display module