标签:return class tco 时间复杂度 ref ted += ble lint
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
LeetCode #1287. Element Appearing More Than 25% In Sorted Array
标签:return class tco 时间复杂度 ref ted += ble lint
原文地址:https://www.cnblogs.com/RatsCommander/p/14035031.html