进制转化、排序、堆
解法一:暴力解法
每insert一个值,加入数组,数组重新排序,再取中位数
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stream=[] def Insert(self, num): # write code here self.stream.append(num) def GetMedian(self,streaming): # write code here streaming=sorted(self.stream) lenth=len(streaming) if lenth%2==0: return (streaming[lenth/2-1]+streaming[lenth/2])/2.0 #注意这里是python2编辑的,默认不除尽,所以要写成2.0保留小数 else: return streaming[lenth//2]
其他解法:
插入排序、堆
原文:https://www.cnblogs.com/foolangirl/p/14152740.html