数组中出现次数超过一半的数字
class Solution:
def majorityElement(self, nums: List[int]) -> int:
# 摩尔计数法,1、互相抵消 2、计数阶段,不等于0则是重复最多的那个
count = 0 # 票数
res = None
for n in nums:
if count == 0:
res = n # 设当前值为大数
count += 1
else:
if n == res: count += 1
else: count -= 1
return res