[leetcode]Moving Average from Data Stream

使用了queue

from queue import Queue

class MovingAverage:

    def __init__(self, size: int):
        """
        Initialize your data structure here.
        """
        self.que = Queue()
        self.size = size
        self.windowSum = 0

    def next(self, val: int) -> float:
        if self.que.qsize() == self.size:
            self.windowSum -= self.que.get()
        self.que.put(val)
        self.windowSum += val
        return self.windowSum / self.que.qsize()
        


# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)

  

posted @ 2020-02-01 18:04  阿牧遥  阅读(110)  评论(0编辑  收藏  举报