美文网首页
LeetCode 1287. Element Appearing

LeetCode 1287. Element Appearing

作者: LiNGYu_NiverSe | 来源:发表于2020-12-04 23:13 被阅读0次

    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.
    给定一个以非降序排序的整数数组,该数组中恰好有一个整数出现超过25%。

    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:

    # solution 1
    from collections import Counter
    class Solution:
        def findSpecialInteger(self, arr: List[int]) -> int:
            return Counter(arr).most_common(1)[0][0]
    
    # solution 2
    class Solution:
        def findSpecialInteger(self, arr: List[int]) -> int:
            return max(set(arr), key = a.count)
    

    Both solution:

    相关文章

      网友评论

          本文标题:LeetCode 1287. Element Appearing

          本文链接:https://www.haomeiwen.com/subject/cprwwktx.html