剑指offer面试题39. 数组中出现次数超过一半的数字(数组)(摩尔投票法)

题目描述

**数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。**
在这里插入图片描述

思路

详见链接

代码

class Solution:
	def majorityElement(self,nums:List[int])->int:
		votes = 0
		for num in nums:
			x = num
			votes += 1 if num == x else -1
		return x
		
发布了195 篇原创文章 · 获赞 566 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_37763870/article/details/105319987