leetcode-15双周赛-1287-有序数组中出现次数超过25%的元素

题目描述:

方法一:二分法

class Solution:
    def findSpecialInteger(self, arr: List[int]) -> int:
        span = len(arr)//4 + 1
        for i in range(0,len(arr),span):
            a = bisect.bisect_left(arr,arr[i])
            b = bisect.bisect_right(arr,arr[i])
            if b - a >= span:
                return arr[i]
        return -1

猜你喜欢

转载自www.cnblogs.com/oldby/p/12084292.html