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

题目

1287. Element Appearing More Than 25% In Sorted Array


解题方法

遍历数组,找到出现次数最多的那个数即可。
时间复杂度:O(n)
空间复杂度:O(1)


代码

class Solution:
    def findSpecialInteger(self, arr: List[int]) -> int:
        rat = 0
        maxcount = 0
        curcount = 0
        curnum = arr[0]
        for i in arr:
            if i == curnum:
                curcount += 1
            else:
                if curcount > maxcount:
                    maxcount = curcount
                    rat = curnum
                curnum = i
                curcount = 1
        else:
            if curcount > maxcount:
                maxcount = curcount
                rat = curnum
        return rat
posted @ 2020-11-25 11:32  老鼠司令  阅读(36)  评论(0编辑  收藏  举报