题解 | #数据流中的中位数#
数据流中的中位数
https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.val=[] def Insert(self, num): # 没有数据就给他插入数据 if len(self.val)==0: self.val.append(num) # 有数据,那排序插入 else: self.val.sort() self.val.append(num) def GetMedian(self): # write code here self.val.sort() length=len(self.val) if length%2!=0: return self.val[length//2] else: num1=self.val[length//2] num2=self.val[length//2-1] return (num1+num2)/2