当前位置:网站首页>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;
边栏推荐
- Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
- [proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
- “用Android复刻Apple产品UI”(3)—优雅的数据统计图表
- June to - -------
- Calibre LVL
- Persisting in output requires continuous learning
- How idea starts run dashboard
- How can technology managers quickly improve leadership?
- Shell script import and export data
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
猜你喜欢
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
Project -- high concurrency memory pool
Record a jar package conflict resolution process
CString getbuffer and releasebuffer instructions
几种常见IO模型的原理
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
WinDbg analysis dump file
How to thicken the brush in the graphical interface
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
Mongodb installation and basic operation
随机推荐
The difference between calling by value and simulating calling by reference
[combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
Stm32f103c8t6 firmware library lighting
QT use qzxing to generate QR code
大csv拆分和合并
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
Remote file contains actual operation
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
[系统安全] 四十三.Powershell恶意代码检测系列 (5)抽象语法树自动提取万字详解
无心剑中译泰戈尔《漂鸟集(1~10)》
首发!!lancet饿了么官方文档
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
How can technology managers quickly improve leadership?
记一次jar包冲突解决过程
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
高等数学(第七版)同济大学 习题2-1 个人解答
嵌入式开发:避免开源软件的7个理由
Myopia: take off or match glasses? These problems must be understood clearly first
Introduction series of software reverse cracking (1) - common configurations and function windows of xdbg32/64