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

Solution in python:

class Solution:
    def findSpecialInteger(self, arr: List[int]) -> int:
        adic = dict()
        for item in arr:
            if item not in adic.keys():
                adic[item] = 1
            else:
                adic[item] += 1
            if adic[item] > len(arr)*0.25:
                return item
最后修改日期: 2021年3月8日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。