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 ...
随机推荐
- Java 动态代理 两种实现方法
AOP的拦截功能是由java中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常时候执 ...
- Java用户输入数值,做简单的猜数字游戏,导入基础的工具包util
Java用户输入数值,做简单的猜数字游戏,导入基础的工具包util,导入包的方法为,import java.util.*: 完整的实例代码: /* 导入基础工具包 */ import java.uti ...
- SVN Commit:将本地代码更新到服务器代码
1.点击客户端“TortoiseSVN” 选中后显示: 点击Import: 点击“ok”:
- JIT物料在途未清PO作为供给
以下是本应在TEMP中处理,目前是写到FP_CHECK_SAP_DATA中 ----JIT物料在途未清PO作为供给 add by landor on 20180809 INSERT INTO SAP_ ...
- Struts学习资料
Strust组件—ActionServlet详解 http://ltc603.iteye.com/blog/68637
- 纯css3棋盘图案背景以及45度斜纹背景
css代码 .stripes { height: 250px; width: 375px; float: left; margin: 10px; ...
- Numpy函数库基础
利用Numpy函数库构造4*4随机数组,然后将数组转化为矩阵,然后矩阵与其逆矩阵相乘,计算机处理的误差 from numpy import * random.rand(4,4) print(rando ...
- laravel5.6上传图片及显示
借鉴大神博客:https://blog.csdn.net/tony_110/article/details/80105099文档:http://laravelacademy.org/post/8965 ...
- 安恒7月赛wp
1.[order] 这道题,发现order参数处有注入点,于是就使用sqlmap盲注,emmmm,学到了sqlmap的一些小窍门. 首先,解题的语句是: sqlmap -u "htt ...
- tcp/ip通信第5期之客户机端程序
/*此程序是tcp/ip通信的客户机端程序, 测试运行在redhat6系统上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> ...