find-median-from-data-stream.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:lc-all-solutions 作者: csujedihy 项目源码 文件源码
def addNum(self, num):
        """
        Adds a num into the data structure.
        :type num: int
        :rtype: void
        """
        left = self.left
        right = self.right
        if self.median is None:
            self.median = num
            return

        if num <= self.median:
            heapq.heappush(left, -num)
        else:
            heapq.heappush(right, num)

        if len(left) > len(right) + 1:
            top = -heapq.heappop(left)
            heapq.heappush(right, self.median)
            self.median = top
        if len(right) > len(left) + 1:
            top = heapq.heappop(right)
            heapq.heappush(left, -self.median)
            self.median = top
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号