边工作边刷题:70天一遍leetcode: day 95-1

Moving Average from Data Stream

https://repl.it/CarT

class MovingAverage(object):

    def __init__(self, size):
        """
        Initialize your data structure here.
        :type size: int
        """
        self.window = collections.deque()
        self.size = size
        self.cur_sum = 0

    def next(self, val):
        """
        :type val: int
        :rtype: float
        """
        if len(self.window)==self.size:
            left = self.window.popleft()
            self.cur_sum-=left
        
        self.window.append(val)
        self.cur_sum+=val
        return self.cur_sum/float(len(self.window))
        


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

posted @ 2016-07-11 20:26  absolute100  阅读(105)  评论(0编辑  收藏  举报