python用的自己实现的最大和最小堆的class,GetMedian需要加个参数,否则python版会报错。# -*- coding:utf-8 -*-
# 最小堆
class minheap:
def __init__(self):
self.minheap = []
def __len__(self):
return len(self.minheap)
def insert(self, elem):
self.minheap.append(elem)
l = len(self.minheap) - 1
while l != 0 and self.minheap[int((l - 1) / 2)] > self.minheap[l]:
self.minheap[int((l - 1) / 2)], self.minheap[l] = self.minheap[l], self.minheap[int((l - 1) / 2)]
l = int((l - 1) / 2)
def pophead(self):
self.minheap[0] = self.minheap[-1]
self.minheap = self.minheap[:-1]
i, l = 0, len(self.minheap) - 1
while 1:
if 2 * i + 1 > l: # 没有子节点
break
elif 2 * i + 2 > l: # 只有左节点
if self.minheap[2 * i + 1]
self.minheap[2 * i + 1], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 1]
i = 2 * i + 1
else:
break
else: # 儿女双全
if self.minheap[2 * i + 1] self.minheap[2 * i + 2]: # 比两个孩子都大
if self.minheap[2 * i + 1]
self.minheap[2 * i + 1], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 1]
i = 2 * i + 1
else:
self.minheap[2 * i + 2], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 2]
i = 2 * i + 2
elif self.minheap[2 * i + 1]
self.minheap[2 * i + 1], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 1]
i = 2 * i + 1
elif self.minheap[2 * i + 1] > self.minheap[i] > self.minheap[2 * i + 2]:
self.minheap[2 * i + 2], self.minheap[i] = self.minheap[i], self.minheap[2 * i + 2]
i = 2 * i + 2
else:
break
def head(self):
return self.minheap[0]
# 最大堆
class maxheap:
def __init__(self):
self.maxheap = []
def __len__(self):
return len(self.maxheap)
def insert(self, elem):
self.maxheap.append(elem)
l = len(self.maxheap) - 1
while l != 0 and self.maxheap[int((l - 1) / 2)]
self.maxheap[int((l - 1) / 2)], self.maxheap[l] = self.maxheap[l], self.maxheap[int((l - 1) / 2)]
l = int((l - 1) / 2)
def pophead(self):
self.maxheap[0] = self.maxheap[-1]
self.maxheap = self.maxheap[:-1]
i, l = 0, len(self.maxheap) - 1
while 1:
if 2 * i + 1 > l: # 没有子节点
break
elif 2 * i + 2 > l: # 只有左节点
if self.maxheap[2 * i + 1] > self.maxheap[i]:
self.maxheap[2 * i + 1], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 1]
i = 2 * i + 1
else:
break
else: # 儿女双全
if self.maxheap[2 * i + 1] > self.maxheap[i]
if self.maxheap[2 * i + 1] > self.maxheap[2 * i + 2]: # 左孩子小于右孩子
self.maxheap[2 * i + 1], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 1]
i = 2 * i + 1
else:
self.maxheap[2 * i + 2], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 2]
i = 2 * i + 2
elif self.maxheap[2 * i + 1] > self.maxheap[i] > self.maxheap[2 * i + 2]:
self.maxheap[2 * i + 1], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 1]
i = 2 * i + 1
elif self.maxheap[2 * i + 1]
self.maxheap[2 * i + 2], self.maxheap[i] = self.maxheap[i], self.maxheap[2 * i + 2]
i = 2 * i + 2
else:
break
def head(self):
return self.maxheap[0]
class Solution:
def __init__(self):
self.minH = minheap()
self.maxH = maxheap()
def Insert(self, num):
self.maxH.insert(num)
if len(self.maxH) > len(self.minH) + 1:
self.minH.insert(self.maxH.head())
self.maxH.pophead()
if len(self.minH) > 0 and self.maxH.head() > self.minH.head():
self.maxH.insert(self.minH.head())
self.minH.insert(self.maxH.head())
self.maxH.pophead()
self.minH.pophead()
def GetMedian(self,n = None):
if len(self.maxH) is len(self.minH):
return float(self.maxH.head() + self.minH.head())/2
elif len(self.maxH) is len(self.minH) + 1:
return self.maxH.head()