2023-12-09 18:42:21 +08:00
|
|
|
<p>给定一个窗口大小和一个整数数据流,根据该滑动窗口的大小,计算滑动窗口里所有数字的平均值。</p>
|
2022-03-27 20:38:29 +08:00
|
|
|
|
|
|
|
<p>实现 <code>MovingAverage</code> 类:</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>MovingAverage(int size)</code> 用窗口大小 <code>size</code> 初始化对象。</li>
|
|
|
|
<li><code>double next(int val)</code> 成员函数 <code>next</code> 每次调用的时候都会往滑动窗口增加一个整数,请计算并返回数据流中最后 <code>size</code> 个值的移动平均值,即滑动窗口里所有数字的平均值。</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
|
|
|
|
<p><strong>示例:</strong></p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>输入:</strong>
|
2023-12-09 18:42:21 +08:00
|
|
|
inputs = ["MovingAverage", "next", "next", "next", "next"]
|
2022-03-27 20:38:29 +08:00
|
|
|
inputs = [[3], [1], [10], [3], [5]]
|
|
|
|
<strong>输出:</strong>
|
|
|
|
[null, 1.0, 5.5, 4.66667, 6.0]
|
|
|
|
|
|
|
|
<strong>解释:</strong>
|
|
|
|
MovingAverage movingAverage = new MovingAverage(3);
|
|
|
|
movingAverage.next(1); // 返回 1.0 = 1 / 1
|
|
|
|
movingAverage.next(10); // 返回 5.5 = (1 + 10) / 2
|
|
|
|
movingAverage.next(3); // 返回 4.66667 = (1 + 10 + 3) / 3
|
|
|
|
movingAverage.next(5); // 返回 6.0 = (10 + 3 + 5) / 3
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
|
|
|
|
<p><strong>提示:</strong></p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>1 <= size <= 1000</code></li>
|
|
|
|
<li><code>-10<sup>5</sup> <= val <= 10<sup>5</sup></code></li>
|
|
|
|
<li>最多调用 <code>next</code> 方法 <code>10<sup>4</sup></code> 次</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
|
|
|
|
<p><meta charset="UTF-8" />注意:本题与主站 346 题相同: <a href="https://leetcode-cn.com/problems/moving-average-from-data-stream/">https://leetcode-cn.com/problems/moving-average-from-data-stream/</a></p>
|