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();
    }
}

 

posted on 2022-04-09 05:25  阳光明媚的菲越  阅读(14)  评论(0编辑  收藏  举报