当前位置:网站首页>Sword finger offer note: T39. Numbers that appear more than half of the time in the array
Sword finger offer note: T39. Numbers that appear more than half of the time in the array
2022-07-27 11:48:00 【Ignorant little nine】
T39. A number that appears more than half the times in an array
There is a number in an array that appears more than half the length of the array , Please find out the number .
You can assume that the array is not empty , And there are always many elements in a given array .
Example 1:
Input : [1, 2, 3, 2, 2, 2, 5, 4, 2]
Output : 2
Limit :
1 <= The length of the array <= 50000
Ideas
Use the technique of dichotomy , The last number in the middle is this number
solution 1 Direct sort
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length>>1];
}
}
Execution time :2 ms, In all Java Defeated in submission **60.35%** Users of
Memory consumption :43.9 MB, In all Java Defeated in submission **9.12%** Users of
solution 2 Moore voted ( clever )
class Solution {
public int majorityElement(int[] nums) {
int n=0, count=0;
for(int num:nums){
if(count==0){
n = num;
}
if(num ==n){
count++;
}else count--;
}
return n;
}
}
Execution time :1 ms, In all Java Defeated in submission **99.97%** Users of
Memory consumption :41.5 MB, In all Java Defeated in submission **87.71%** Users of
Time complexity :O(n), Spatial complexity :O(1)
边栏推荐
- Maker Hongmeng application development training notes 02
- EfficientNet
- LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
- 局域网SDN技术硬核内幕 13 从局域网到互联网
- 微博评论爬虫+可视化
- [machine learning whiteboard derivation series] learning notes --- conditional random fields
- 第7章 异常处理
- [unity entry program] creator kitfps: first person shooting 3D game
- 剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
- 剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
猜你喜欢

美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生

origin如何作一张图中多张子图是柱状图(或其他图)

Everything cannot be searched for startup_ Lpc11x.s file

Source code compilation and installation lamp

PWM的原理和PWM波的产生

Moveit2 -- 2. Quick start of moveit in rviz

Codeforces round #664C

Modelarts voice detection and text classification

第10章 枚举类与注解
新版数据仓库的同步使用参考(新手向)
随机推荐
为什么TCP三次握手的时候ACK=Seq+1
[machine learning whiteboard derivation series] learning notes --- conditional random fields
Modelarts voice detection and text classification
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
LeetCode-SQL练习题总结(MySQL实现)
The C programming language 2nd -- Notes -- 6.7
A possibility that ch340 module cannot be recognized / burned
Tlc549proteus simulation &sallen key filter &ad736vrms to DC conversion &proteus view 51 register value
The C programming language (2nd) -- Notes -- 1.8
Local virtual machine initialization script
检定和校准的区别
USB 网卡驱动数据流
The difference between microcomputer and single chip microcomputer
Everything cannot be searched for startup_ Lpc11x.s file
多种进制之间的转换
【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏
Temporary use of solo, difficult choice of Blog
Moveit2 - 5. Scenario Planning
Common power supply problems and solutions of Arduino
微机和单片机的区别