[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 self.que.qsize() == self.size:
            self.windowSum -= self.que.get()
        self.que.put(val)
        self.windowSum += val
        return self.windowSum / self.que.qsize()

# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)

  

原文地址:https://www.cnblogs.com/lautsie/p/12249345.html

时间: 2024-08-14 06:48:54

[leetcode]Moving Average from Data Stream的相关文章

[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

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

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 +