Problem Link
Description
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:1
2
3
4
5MovingAverage 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
Solution
1 | class MovingAverage { |
Sth else
复习队列的第一道题,写的时候有一点卡,大概是时隔半年终于重新开始跟代码死磕了。一开始没太明白这题想干嘛,就是隐约有个思路,然后就推不动了,差点自己实现一波队列…我知道这是个简单题,然而…现实就是我卡了一天。希望后续会好一点(我应该会写一段时间的注释ovo)
test