leetcod求众数

mac2022-06-30  42

题目描述: 给定一个大小为 n的数组,找到其中的众数,众数是指在数组中出现次数大于[n/2]的元素。你可以假设数组是非空的,并且给定的数组总是存在众数。

def majorityElement(self,nums): " " " :type nums:List[int] :rtype:int " " " tmp=Counter(nums).most_common() for i in range(len(tmp): if tmp[i][1] >len(nums)/2: return tmp[i][0]
最新回复(0)