leetcode346. 数据流中的移动平均值

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

示例:

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

思路:一个队列记录数字,用一个变量记录窗口和即可,每次更新窗口和。

class MovingAverage {
  int size//窗口大小
  int windowSum = 0//窗口和
  int count = 0;//添加数字的次数
  Deque queue = new ArrayDeque<Integer>();
    
  public MovingAverage(int size) {
    this.size = size;
  }

  public double next(int val) {
    ++count;
    // calculate the new sum by shifting the window
    queue.add(val);
    //看有没有过期的数字(最左边)
    int tail = count > size ? (int)queue.poll() : 0;
    //更新窗口和
    windowSum = windowSum - tail + val;

    return windowSum * 1.0 / Math.min(size, count);
  }
}
/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */
发布了484 篇原创文章 · 获赞 1万+ · 访问量 111万+

猜你喜欢

转载自blog.csdn.net/hebtu666/article/details/104103522