File tree 4 files changed +52
-22
lines changed
1287. Element Appearing More Than 25% In Sorted Array
1287.Element Appearing More Than 25% In Sorted Array
4 files changed +52
-22
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ ## 1287. 有序数组中出现次数超过 25% 的元素
2
+ 给你一个非递减的 有序 整数数组,已知这个数组中恰好有一个整数,它的出现次数超过数组元素总数的 25%。
3
+
4
+ 请你找到并返回这个整数。
5
+
6
+ - JavaScript
7
+
8
+ ``` javascript
9
+ const findSpecialInteger = function (arr ) {
10
+ let count = 0 ;
11
+ let item = - 1 ;
12
+ for (var i = 0 ; i < arr .length ; i++ ) {
13
+ if (item == arr[i]) {
14
+ count++ ;
15
+ } else {
16
+ item = arr[i];
17
+ count = 1 ;
18
+ }
19
+ if (count > arr .length * 0.25 ) {
20
+ return item;
21
+ }
22
+ }
23
+ return item;
24
+ };
25
+ ```
26
+
27
+ - Java
28
+
29
+ ``` java
30
+ class Solution {
31
+ public int findSpecialInteger (int [] arr ) {
32
+ int total = arr. length;
33
+ for (int i = 0 ; i < total; ++ i) {
34
+ if (arr[i] == arr[i + (total >> 2 )]) {
35
+ return arr[i];
36
+ }
37
+ }
38
+ return 0 ;
39
+ }
40
+ }
41
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findSpecialInteger (int [] arr ) {
3
+ int total = arr .length ;
4
+ for (int i = 0 ; i < total ; ++i ) {
5
+ if (arr [i ] == arr [i + (total >> 2 )]) {
6
+ return arr [i ];
7
+ }
8
+ }
9
+ return 0 ;
10
+ }
11
+ }
File renamed without changes.
You can’t perform that action at this time.
0 commit comments