LeetCode-1287. Element Appearing More Than 25% In Sorted Array

网友投稿 320 2022-08-29

LeetCode-1287. 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​​

​​题解:​​

​​连续的数组那么出现25%的肯定有num[i] = num[i + n / 4]​​

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:营销扶不起《斛珠夫人》,但平台的“古偶上瘾症”远未结束!(豆瓣斛珠夫人)
下一篇:LeetCode-1288. Remove Covered Intervals
相关文章

 发表评论

暂时没有评论,来抢沙发吧~