数组中出现次数超过一半的数字
作者:
momo1023 | 来源:发表于
2019-03-27 19:11 被阅读0次# -*- coding:utf-8 -*-
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
l = len(numbers)
if l <= 0:
return 0
cur = numbers[0]
count = 1
for i in range(1, l):
if count == 0:
cur = numbers[i]
count = 1
else:
if cur == numbers[i]:
count += 1
else:
count -= 1
# 验证
count = 0
for num in numbers:
if num == cur:
count += 1
if count > l / 2:
return cur
else:
return 0
本文标题:数组中出现次数超过一半的数字
本文链接:https://www.haomeiwen.com/subject/hxptbqtx.html
网友评论