思路:
答案是多数,那么用少数去抵消多数的票一定剩的还是多数,于是得到多数的答案
代码:
from typing import List
class Solution:
def majorityElement(self, nums: List[int]) -> int:
votes = 0
for num in nums:
if votes == 0:
x = num
votes += 1 if num == x else -1
return x