346. Moving Average from Data Stream

346. Moving Average from Data Stream

难度: Easy

刷题内容

原题连接

内容描述

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Example:

MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3

解题方案

思路 1
- 时间复杂度: O(1)- 空间复杂度: O(1)

beats 100%

top Created with Sketch.