一、leetcode地址
https://leetcode.com/problems/majority-element/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def majorityElement(self, nums: List[int]) -> int:
stack = []
for num in nums:
if not stack or num == stack[-1]:
stack.append(num)
else:
stack.pop()
return stack[-1]