仅一次遍历,清晰明了的Python代码,类似题目在力扣中也有。
class Solution: def MoreThanHalfNum_Solution(self, numbers: List[int]) -> int: # write code here cnt = 1 res = numbers[0] for i in numbers[1:]: if i == res: cnt += 1 elif cnt == 0: res = i cnt = 1 else: cnt -= 1 return res