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) {
q = new LinkedList();
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. [RT][NOIP2015]联合权值

1.题面 2.总结 第一次回忆一下当年的题目.但是这道题已经做烂了,只是看还记得树遍历会写么. 然后我写了一下,有点费劲,交上去之后只有70,比较尴尬,看了下去年5月写的代码,发现完全不是一个感觉啊. ...

2. JS中的常量（基本数据类型）和内置对象

JS中的基本数据类型: String .number.null.boolean.undefined.object.symbol(ES6) 1.  利用typeof运算符时其中只有null是异常的,ty ...

3. 未找到导入的项目，请确认 &lt;Import&gt; 声明中的路径正确

当使用vs出现下列情况: D:\xxxx\Web\Web.csproj : error  : 无法读取项目文件“Web.csproj”. D:\xxxx\WebServiceManager\Web\W ...

4. QTP学习笔记之—VBS

1.ToString() : Returns a string that represents the current test object. Example The following examp ...

5. 把HBITMAP 保存图片文件.

//VC下把HBITMAP保存为bmp图片 BOOL CtestBitMapDlg::SaveBmp(HBITMAP hBitmap, CString FileName) { HDC hDC; //当 ...

6. mysql metadata lock(一)

想必玩过mysql的人对Waiting for table metadata lock肯定不会陌生,一般都是进行alter操作时被堵住了,导致了我们在show processlist 时,看到线程的状 ...

7. 利用opencv进行相机标定程序

#include "Stafx.h" ; //棋盘上有13个格子,那么角点的数目12 ; ; //图片的总张数 int main(int argc, char** argv) { ...

8. 各浏览器抗uaf机制

今年中旬,微软针对旗下ie浏览器中大量出现的uaf漏洞,对ie浏览器的安全机制进行了一个大幅度的升级,其中主要体现为隔离堆及延迟释放两个机制,顿时又将uaf漏洞的利用向上提升了一个大坎, 但是类似的对 ...

9. 第十章：鸟哥的Linux私房菜

第十章.vim程式编辑器 1. vi与vim 1.1 为何要学vim2. vi的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案例的练习 2.4 vim的暂存档.救援回复与开启时的警告讯息 ...

10. 升级Xcode6.4插件失效解决办法

发现安装的插件无效 后各种尝试后终于解决 1.先找到之前装的插件文件夹 前往文件夹:~/Library/Application Support/Developer/Shared/Xcode/Plug- ...