Moving Average from Data Stream(Easy)
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
For 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
Java 实现
Queue<Integer> q;
int size = 0;
int sum = 0;
/** Initialize your data structure here. */
public MovingAverage(int size) {
q = new LinkedList<>();
this.size = size;
}
public double next(int val) {
q.offer(val);
sum += val;
if(q.size() > size) {
sum -= q.peek();
q.poll();
}
return 1.0 * sum / q.size();
}