工作生活

346-数据流中的移动平均值

2019-07-03  本文已影响0人  饮酒醉回忆

数据流中的移动平均值

题目

给定一个整数数据流和一个窗口大小,根据该滑动窗口的大小,计算其所有整数的移动平均值。

示例:

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

思路

内部实现使用一个队列来做,判断当前队列大小和限定大小,将总和加减,之后算平均数

代码

public class MovingAverage {
    private double previousSum = 0.0;
    private int maxSize;
    private Queue<Integer> currentWindow;
    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        currentWindow = new LinkedList<Integer>();
        maxSize = size;
    }
     
    public double next(int val) {
        if(currentWindow.size()==maxSize){
            previousSum -= currentWindow.remove();
        }
        currentWindow.add(val);
        previousSum += val;
        return previousSum/currentWindow.size();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读