/*
* 346. Moving Average from Data Stream
* 2016-7-11 by Mingyang
* 这里注意的就是(double) sum / count
* sum需要转换成double才能继续往下除,因为不转的话最后不能成为整数14除以3等于4
*/
class MovingAverage {
public Queue<Integer> queue;
public int sum = 0;
public int si;
public int count = 0;
/** Initialize your data structure here. */
public MovingAverage(int size) {
this.si = size;
queue = new LinkedList<Integer>();
}
public double next(int val) {
if (queue.size() < si) {
queue.add(val);
sum += val;
count++;
} else {
int temp = queue.poll();
sum -= temp;
queue.add(val);
sum += val;
}
return (double) sum / count;
}
}