0
点赞
收藏
分享

微信扫一扫

1287. Element Appearing More Than 25% In Sorted Array*

1287. Element Appearing More Than 25% In Sorted Array*

​​https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/​​

题目描述

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.

Return that integer.

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6

Constraints:

  • ​1 <= arr.length <= 10^4​
  • ​0 <= arr[i] <= 10^5​

C++ 实现 1

class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int i = 0, n = arr.size() / 4;
while (i < arr.size()) {
int j = i + 1;
while (j < arr.size() && arr[j] == arr[i]) ++ j;
if (j - i > n) return arr[i];
i = j;
}
return -1;
}
};

扩展阅读

使用二分搜索进行求解的: ​​Java Binary Search​​

举报

相关推荐

0 条评论