题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
代码实现
# -*- coding:utf-8 -*-
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
lenth=len(numbers)
cot=int(lenth/2)
if lenth==0:
return 0
res=[]
for i in numbers:
if numbers.count(i)>cot:
res.append(i)
res=list(set(res))#python去掉列表中重复的元素
if len(res)==0:
return 0
else:
return res[0]