forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingAverage.java
More file actions
24 lines (19 loc) · 522 Bytes
/
MovingAverage.java
File metadata and controls
24 lines (19 loc) · 522 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Deque;
import java.util.LinkedList;
public class MovingAverage {
private int mSize;
private double mSum;
private Deque<Integer> mQueue = new LinkedList<>();
/** Initialize your data structure here. */
public MovingAverage(int size) {
mSize = size;
}
public double next(int val) {
mQueue.offerLast(val);
mSum += val;
if (mQueue.size() > mSize) {
mSum -= mQueue.pollFirst();
}
return mSum / mQueue.size();
}
}