其他分享
首页 > 其他分享> > 346. Moving Average from Data Stream

346. Moving Average from Data Stream

作者:互联网

class MovingAverage {
    Queue<Integer> queue = new LinkedList<>();
    int size = 0;
    double sum=0;
    public MovingAverage(int size) {
        this.size = size;
    }
    
    public double next(int val) {
        if(queue.size()==size){
            sum-=queue.poll();
        }
        queue.offer(val);
        sum+=val;
        return sum/queue.size();
    }
}

 

标签:val,int,double,sum,queue,346,Moving,Data,size
来源: https://www.cnblogs.com/feiflytech/p/16120716.html