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 = 0;

    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        this.si = size;
        queue = new LinkedList<Integer>();
    }

    public double next(int val) {
        if (queue.size() < si) {
            queue.add(val);
            sum += val;
            count++;
        } else {
            int temp = queue.poll();
            sum -= temp;
            queue.add(val);
            sum += val;
        }
        return (double) sum / count;
    }
}
时间: 2024-10-14 18:16:40

346. Moving Average from Data Stream的相关文章

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

[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 +

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

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] 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