题解 | #数据流中的中位数#
数据流中的中位数
https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
# -*- coding:utf-8 -*-
val = []
class Solution:
def Insert(self, num):
# write code here
if len(val) == 0:
val.append(num)
else:
i = 0
while i < len(val):
if num > val[i]:
i += 1
else:
break
val.insert(i, num)
def GetMedian(self):
# write code here
l = len(val)
if l % 2 == 1:
return val[l//2]
else:
return (val[l//2] + val[l//2-1]) / 2
查看12道真题和解析
华为技术有限公司工作强度 1291人发布