Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

For example,
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

分析:

利用Queue先进先出的特点即可。

 1 public class MovingAverage {
 2     Queue<Integer> q;
 3     double sum = 0;
 4     int size;
 5
 6     /** Initialize your data structure here. */
 7     public MovingAverage(int s) {
 8         q = new LinkedList();
 9         size = s;
10     }
11
12     public double next(int val) {
13         if (q.size() == size) {
14             sum = sum - q.poll();
15         }
16         q.offer(val);
17         sum += val;
18         return sum / q.size();
19     }
20 }
时间: 2024-10-13 11:47:58

Moving Average from Data Stream的相关文章

346. Moving Average from Data Stream

/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * sum需要转换成double才能继续往下除,因为不转的话最后不能成为整数14除以3等于4 */ class MovingAverage { public Queue<Integer> queue; public int sum = 0; public int si; public int count

Leetcode 346. Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, 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) = (1

LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, 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) = (1

Moving Average from Data Stream -- LeetCode

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, 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) = (1

[leetcode]346. Moving Average from Data Stream滑动窗口平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, 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) = (1

[LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example,MovingAverage m = new MovingAverage(3);m.next(1) = 1m.next(10) = (1 + 10) / 2m.next(3) = (1 + 10 + 3) / 3m.next(5) = (10 + 3

346. Moving Average from Data Stream - Easy

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Example: 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 +

[LeetCode] Moving Average from Data Stream 从数据流中移动平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example,MovingAverage m = new MovingAverage(3);m.next(1) = 1m.next(10) = (1 + 10) / 2m.next(3) = (1 + 10 + 3) / 3m.next(5) = (10 + 3

[leetcode]Moving Average from Data Stream

使用了queue from queue import Queue class MovingAverage: def __init__(self, size: int): """ Initialize your data structure here. """ self.que = Queue() self.size = size self.windowSum = 0 def next(self, val: int) -> float: if