当前位置:网站首页>【刷题篇】多数元素(超级水王问题)
【刷题篇】多数元素(超级水王问题)
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;
}
边栏推荐
- Debug: CD cannot be used in kaggle
- 编译文件时报错:错误: 编码GBK的不可映射字符
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- sigaction的使用
- 递归:深度优先搜索
- pytorch怎么下载?pytorch在哪里下载?
- 2022 P cylinder filling examination content and P cylinder filling practice examination video
- Dynamic programming: longest common substring and longest common subsequence
- [Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
- 释放数据力量的Ceph-尚文网络xUP楠哥
猜你喜欢

MongoDB简介

105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode

递归:深度优先搜索

How to download pytorch? Where can I download pytorch?

Download and install node, NPM and yarn

PHP generates PDF tcpdf

没有sXid,suid&sgid将进入险境!-尚文网络xUP楠哥

Table structure of Navicat export database

pytorch怎么下载?pytorch在哪里下载?
![[embedded module] OLED display module](/img/c4/474f5ee580d132654fbd1a4cd53bab.jpg)
[embedded module] OLED display module
随机推荐
The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
FileZilla client download and installation
Message queue addition failure
What is pytorch? Is pytorch a software?
pytorch难学吗?如何学好pytorch?
Table structure of Navicat export database
ffmpeg之 一张/多张图片合成视频
MongoDB基本操作【增、删、改、查】
golang xxx. Go code template
PHP generates PDF tcpdf
Web session management security issues
小程序获取用户头像和昵称
Basic operations of mongodb [add, delete, modify, query]
在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
Recursive use and multi-dimensional array object to one-dimensional array object
递归:一维链表和数组
Ansible introduction [unfinished (semi-finished products)]
Summary of electromagnetic spectrum
Ansible简介【暂未完成(半成品)】