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

``` public class MovingAverage {
Queue<Integer> q;
;
int size;

/** Initialize your data structure here. */
public MovingAverage(int s) {
size = s;
}

public double next(int val) {
if (q.size() == size) {
sum = sum - q.poll();
}
q.offer(val);
sum += val;
return sum / q.size();
}
}```

## Moving Average from Data Stream的更多相关文章

1. LeetCode Moving Average from Data Stream

原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

2. 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 ...

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

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

4. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

5. [LeetCode] Find Median from Data Stream 找出数据流的中位数

Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

6. Find Median from Data Stream

常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

7. 数据结构与算法(1)支线任务8——Find Median from Data Stream

题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

8. [LeetCode] Find Median from Data Stream

Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...

9. LeetCode——Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

## 随机推荐

1. 前端常用的几个js判断（二）

10.鼠标悬停(hover)切换 class 属性假如当用户鼠标悬停在一个可点击的元素上时,你希望改变其效果,下面这段代码可以在其悬停在元素上时添加 class 属性,当用户鼠标离开时,则自动取消该 ...

2. htmL5 html5Validate

http://www.zhangxinxu.com/wordpress/2012/12/jquery-html5validate-html5-form-validate-plugin/

3. Java并发编程-CopyOnWriteArrayList

CopyOnWriteArrayList原理 首先每次写操作,都将数组copy一份,并赋值给arrays 读操作读不加锁 写操作加锁 ReentrantLock 因为每次写都要copy数组,这是一项繁 ...

4. SQL语句优化原则

处理百万级以上的数据提高查询速度的方法: .应尽量避免在 .对查询进行优化,应尽量避免全表扫描,首先应考虑在 .应尽量避免在   .应尽量避免在  or num=      可以这样查询:       ...

5. Java static解析

static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块 一.static变量 被static修饰的成员变量和成员方法独立于该类的任何对象.也就是说,它 ...

6. Android去除CPU占用过高时屏幕四周闪红框

话说有些时间没有更新博客了,今天正好解决这个问题,顺便把它记录下来.. 今天遇到的情况是这样的,当CPU占用过高时,屏幕四周会出现一个红框. 闪一次两次算了,但是挺萌的(TMD)不停的闪,我的钛合金狗 ...

7. 分析一下FastDFS_java_client中TestClient.java这个文件以及跟它关联的这条线

本来先打算上个图来说明一下这条线的,可是我的画图工具还没有安装好,我先把跟TestClient.java相关的几个文件代码贴上来,但是由于代码行数还是不少的,所以请大家阅读文章的时候先不要展开代码,等 ...

8. 【转】Maven实战（六）--- dependencies与dependencyManagement的区别

原博文出自于:http://blog.csdn.net/liutengteng130/article/details/46991829   感谢! 在上一个项目中遇到一些jar包冲突的问题,之后还有很 ...

9. 导入MyEclipse项目乱码

1.右键项目属性→text file encoding →修改为UTF-8,即可自动变成正常的.

10. 幻灯片の纯CSS，NO JavaScript

之前就遇到有人问,不用js,纯css实现幻灯片. 那么对于使用纯的css + html 怎样来实现幻灯片呢?下面有几种方法可供参考,有些还不成熟. 方案一:利用css3的animation 例子传送门 ...