🟢 剑指 Offer II 041. 滑动窗口的平均值
LeetCode 提示
题目难度 简单
原题链接 🔗 leetcode
#
题解1class MovingAverage { int sum; int size; Deque<Integer> queue;
/** Initialize your data structure here. */ public MovingAverage(int size) { this.sum = 0; this.size = size; this.queue = new LinkedList<>(); } public double next(int val) { this.queue.add(val); this.sum += val; if (this.queue.size() > this.size) { int top = this.queue.poll(); this.sum -= top; }
return (double) this.sum / this.queue.size(); }}
/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */