346. Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3
解题要点:
让一个queue永远保持不要超过size。
class MovingAverage(object):
def __init__(self, size):
"""
Initialize your data structure here.
:type size: int
"""
self.queue = []
self.size = size
def next(self, val):
"""
:type val: int
:rtype: float
"""
if len(self.queue) == self.size:
self.queue.pop(0)
self.queue.append(val)
return float(sum(self.queue)) / float(len(self.queue))
# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)
Last updated
Was this helpful?