当前位置:网站首页>1287. Elements that appear more than 25% in an ordered array
1287. Elements that appear more than 25% in an ordered array
2022-07-03 16:07:00 【what's your name.】
【 Simple 】 Give you a non decreasing Orderly An array of integers , It is known that there is exactly one integer in this array , It occurs more than the total number of elements in the array 25%.
Please find and return this integer
Example :
Input :arr = [1,2,2,6,6,6,6,7,10]
Output :6
Tips :
1 <= arr.length <= 10^4
0 <= arr[i] <= 10^5
Solution 1 :
public int m1(int[] arr) {
int target = 0;
int start = 0;
int x = arr.length / 4;//25%
for (int i = 0; i < arr.length; i++) {
if (arr[i] != target) {
start = i;
target = arr[i];
}
if (i - start + 1 > x)
return target;
}
return target;
}
Solution 2 :
public int m2(int[] arr) {
int threshold = arr.length / 4;//25%
for (int i = 0; i < arr.length; i++) {
if(arr[i + threshold] == arr[i])
return arr[i];
}
return 0;
}
Solution 3 : Hash map
Map<Integer, Integer> map = new HashMap();
for(int i : arr){
// First, store the data in the array in map in ,key Element value ,value Is the number of occurrences
map.put(i, map.getOrDefault(i, 0) + 1);
}
for(int key : map.keySet()){
Integer value = map.get(key);
if(value > arr.length / 4){
// Timely output if there are satisfied
return key;
}
}
return -1;
边栏推荐
- Persisting in output requires continuous learning
- ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
- App移动端测试【4】apk的操纵
- 几种常见IO模型的原理
- Microservices Seata distributed transactions
- The difference between RAR and zip files
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- Automatic generation of client code from flask server code -- Introduction to flask native stubs Library
- MongoDB 的安装和基本操作
- A Fei's expectation
猜你喜欢

Detailed explanation of string function and string function with unlimited length

Microservice API gateway

Semi supervised learning

Myopia: take off or match glasses? These problems must be understood clearly first

Mongodb installation and basic operation

Reading notes of "micro service design" (Part 2)

Unity function - unity offline document download and use

Colab works with Google cloud disk

Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword

Vs2017 is driven by IP debugging (dual machine debugging)
随机推荐
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
使用AUR下载并安装常用程序
半监督学习
Redis高可用与持久化
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
How can technology managers quickly improve leadership?
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
一些事情的反思
The difference between calling by value and simulating calling by reference
App mobile terminal test [3] ADB command
首发!!lancet饿了么官方文档
TCP拥塞控制详解 | 3. 设计空间
Mb10m-asemi rectifier bridge mb10m
"Everyday Mathematics" serial 56: February 25
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
First knowledge of database
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
Detailed explanation of four modes of distributed transaction (Seata)