class Solution:
def majorityElement(self, nums: List[int]) -> int:
candidate = None
count = 0
for num in nums:
if count == 0:
candidate = num
if num == candidate:
count += 1
else:
count -= 1
return candidate
每日一道Leetcode - 169. 多数元素 [哈希字典|摩尔投票法]
最新推荐文章于 2024-07-23 14:36:35 发布