Moving Average from Data Stream LT346
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
public class MovingAverage {
private Queue<Integer> queue;
private int capacity;
private double sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
queue = new ArrayDeque<>();
capacity = size;
sum = 0;
} public double next(int val) {
if(queue.size() == capacity) {
sum -= queue.poll();
}
queue.offer(val);
sum += val;
return sum/queue.size();
} public static void main(String[] args) {
int[] nums = {1, 10, 3, 5};
MovingAverage movingAverage = new MovingAverage(3);
for(int num: nums) {
System.out.println(movingAverage.next(num));
}
}
}
python:
import queue class MovingAverage:
def __init__(self, capacity):
self.capacity = capacity
self.sum = 0
self.window = queue.Queue(capacity) def next(self, num):
if self.window.full():
self.sum -= self.window.get() self.sum += num
self.window.put(num)
return self.sum/self.window.qsize() def test():
test_data = [1, 10, 3, 5]
test_subject = MovingAverage(3)
for num in test_data:
print (test_subject.next(num)) if __name__ == '__main__':
test()
Moving Average from Data Stream LT346的更多相关文章
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- 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 sl ...
- [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 sl ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- Moving Average from Data Stream -- LeetCode
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- [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 sl ...
- 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
随机推荐
- 1.5.4、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置自定义Java主目录位置)
配置自定义Java主目录位置 注意: Cloudera强烈建议安装JDK/ usr / java / jdk-version,允许Cloudera Manager自动检测并使用正确的JDK版本.如果在 ...
- 1.3.4、CDH 搭建Hadoop在安装之前(端口---Impala使用的端口)
Impala使用的端口 Impala使用下表中列出的TCP端口.在部署Impala之前,请确保在每个系统上打开这些端口. Component Service Port Access Requireme ...
- ssh远程端口转发
当ssh的连接方向和应用连接的方向不一致时,这就称为ssh远程转发. 主机3是一台web server 应用请求是主机2到主机1 ssh请求是主机1到主机2 主机2开启ssh服务 service ss ...
- CSS3 Backgrounds相关介绍
CSS3 Backgrounds相关介绍 1.背景图片(background images)是在padding-box的左上角落脚安家的,我们可以使用background-position属性改变默认 ...
- xshell 5 书写汉字乱码
使用win10安装xshell5后发现使用xshell连接虚拟机后,能识别中文但是无法输入中文了 控制面板--语言 删除除中文输入法之外的所有输入法和美式键盘 然后就可以正常输入中文了,为了英 ...
- leetcode 中等题(1)
2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- pta_l1-6(连续因子)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805138600869888 题意:给定n,求n的最长的连续子因 ...
- hdoj1013(数根,大数,九余数算法)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- BGRA与BGR的相互转换
BGRA转BGR void BgraToBgr(BYTE *bgraData,int *bgraSize) { ,j=; j<*bgraSize; i+=,j+=) { *(bgraData+i ...
- Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)
Devexpress Gridview 提供了简单的求和,平均等方法,复杂的汇总方法则需要自定义,使用gridview 的CustomSummaryCalculate 事件,根据官网的文档及各论坛案例 ...