码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Moving Average from Data Stream

时间:2020-02-01 19:17:53      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:str   ini   queue   from   instant   called   float   python   +=   

使用了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)

  

[leetcode]Moving Average from Data Stream

标签:str   ini   queue   from   instant   called   float   python   +=   

原文地址:https://www.cnblogs.com/lautsie/p/12249345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!