原题链接:
https://leetcode.cn/problems/qIsx9U/

解法1:

队列 按照题意模拟即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

class MovingAverage {
public:
queue<int>q;
int s;
double cur = 0;
/** Initialize your data structure here. */
MovingAverage(int size) {
s = size;
}

double next(int val) {
q.push(val);
cur += val;
if(q.size() > s){
cur -= q.front();
q.pop();
}

return cur / q.size();
}
};

/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage* obj = new MovingAverage(size);
* double param_1 = obj->next(val);
*/