当前位置:网站首页>[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;
}
边栏推荐
- 2022年已过半,得抓紧
- leetcode:动态规划模板
- MPLS setup experiment
- 递归:一维链表和数组
- SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
- [learning notes] seckill - seckill project - (11) project summary
- golang xxx. Go code template
- [mathematical logic] predicate logic (first-order predicate logic formula | example)
- Write it down once Net travel management background CPU Explosion Analysis
- MySQL MAC download and installation tutorial
猜你喜欢
![[home push IMessage] software installation virtual host rental tothebuddy delay](/img/e7/eb20a773e4b674962f856d179a3769.jpg)
[home push IMessage] software installation virtual host rental tothebuddy delay

2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank

How does the pytorch project run?

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

2022 mobile crane driver examination registration and mobile crane driver operation examination question bank

递归:一维链表和数组

Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi

Is pytorch difficult to learn? How to learn pytorch well?

What can learning pytorch do?

docker安装及启动mysql服务
随机推荐
105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
『期末复习』16/32位微处理器(8086)基本寄存器
@The difference between Autowired, @qualifier, @resource
Use of sigaction
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
基于Pytorch和RDKit的QSAR模型建立脚本
Open Visual Studio 2010 hangs when opening a SQL file sql file
Web session management security issues
Separable bonds and convertible bonds
How to execute a swift for in loop in one step- How can I do a Swift for-in loop with a step?
Appium automated testing framework
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
[Yu Yue education] reference materials of political communication science of Communication University of China
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
[mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
[learning notes] seckill - seckill project - (11) project summary
Half of 2022 is over, so we must hurry up
In Net 6 project using startup cs