1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/数据流中的移动平均值 [qIsx9U].html

42 lines
1.6 KiB
HTML
Raw Normal View History

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>&nbsp;成员函数 <code>next</code>&nbsp;每次调用的时候都会往滑动窗口增加一个整数,请计算并返回数据流中最后 <code>size</code> 个值的移动平均值,即滑动窗口里所有数字的平均值。</li>
</ul>
<p>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= size &lt;= 1000</code></li>
<li><code>-10<sup>5</sup> &lt;= val &lt;= 10<sup>5</sup></code></li>
<li>最多调用 <code>next</code> 方法 <code>10<sup>4</sup></code></li>
</ul>
<p>&nbsp;</p>
<p><meta charset="UTF-8" />注意:本题与主站 346&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/moving-average-from-data-stream/">https://leetcode-cn.com/problems/moving-average-from-data-stream/</a></p>