🔴 剑指 Offer 41. 数据流中的中位数
LeetCode 提示
题目难度 困难
原题链接 🔗 leetcode
#
题解 1_抄的.pyfrom heapq import *
class MedianFinder:
def __init__(self): """ initialize your data structure here. """ self.left = [] self.right = []
def addNum(self, num: int) -> None: if len(self.left) == len(self.right): heappush(self.left, -heappushpop(self.right, num)) else: heappush(self.right, -heappushpop(self.left, -num))
def findMedian(self) -> float: return -self.left[0] if len(self.left) != len(self.right) else (self.right[0]-self.left[0])/2
# Your MedianFinder object will be instantiated and called as such:# obj = MedianFinder()# obj.addNum(num)# param_2 = obj.findMedian()