给出一串整数流和窗口大小,计算滑动窗口所有整数的平均值。
样例:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1 // 返回 1.00000
m.next(10) = (1 + 10) / 2 // 返回 5.50000
m.next(3) = (1 + 10 + 3) / 3 // 返回 4.66667
m.next(5) = (10 + 3 + 5) / 3 // 返回 6.00000
class MovingAverage(object):
"""
@param: size: An integer
"""
def __init__(self, size):
# do intialization if necessary
self.size = size
self.list_ = []
self.cur_sum = 0
"""
@param: val: An integer
@return:
"""
def next(self, val):
# write your code here
if len(self.list_)+1<self.size:
self.cur_sum += val
self.list_.append(val)
ans = self.cur_sum/len(self.list_)
else:
self.cur_sum += val
self.list_.append(val)
ans = self.cur_sum/len(self.list_)
self.cur_sum -= self.list_.pop(0)
return ans
# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param = obj.next(val)