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) = (10 + 3 + 5) / 3
class MovingAverage {
private:
queue<int> numsInWindow;
int windowSize;
double lastAverage;
public:
/** Initialize your data structure here. */
MovingAverage(int size) {
windowSize = size;
} double next(int val) {
if (numsInWindow.empty()) {
numsInWindow.push(val);
lastAverage = (double) val;
}
else if (numsInWindow.size() < windowSize) {
double total = lastAverage * numsInWindow.size();
numsInWindow.push(val);
lastAverage = (total + val) / numsInWindow.size();
}
else {
double total = lastAverage * numsInWindow.size();
total -= numsInWindow.front();
numsInWindow.pop();
numsInWindow.push(val);
lastAverage = (total + val) / numsInWindow.size();
}
return lastAverage;
}
}; /**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/
Moving Average from Data Stream -- LeetCode的更多相关文章
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- 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 ...
- [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 ...
- [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 ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- [Swift]LeetCode346. 从数据流中移动平均值 $ Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
随机推荐
- Python利器一之requests
Python利器一之requests 一.教程涉及开发语言.脚本.框架.数据库等内容 Python + requests 通过 pip 安装: pip install requests 通过 easy ...
- jekens介绍及服务搭建
https://blog.csdn.net/achuo/article/details/51086599 https://blog.csdn.net/qq_37372007/article/detai ...
- python中字符串是特殊的列表
for x in range(20): print 'fizz'[x%3*4::]+'buzz'[x%5*4::]or x 这个是由 Jeff Atwood推广的一个编程练习叫FizzBuzz,问题如 ...
- [译]11-spring bean定义的继承
spring中bean的定义包含很多信息,如,构造器参数.property指定的依赖项.初始化方法.工厂类和工厂方法等. 如果spring容器的中每个bean都重复声明这些属性,是非常烦人也是十分低效 ...
- winform-实现类似QQ停靠桌面上边缘隐藏的效果
//实现类似QQ停靠桌面上边缘隐藏的效果! private void timer1_Tick(object sender, EventArgs e) { System.Drawing.Point pp ...
- KMP与循环节相关题目
HDU 3746 Cyclic Nacklace ( KMP求最小循环节 ) len - nextval[len]即为最小循环节长度. #include <cstdio> #include ...
- Batting Practice LightOJ - 1408
Batting Practice LightOJ - 1408(概率dp) 题意:有无限个球,进球的概率为p,问你连续不进k1个球或者连续进k2个球需要使用的球的个数的期望 思路: \(定义f[i]表 ...
- jquery.jbox JBox-v2.3修改版
原版jquery.jbox是个不错的jquery扩展,使用简单,功能很多.可惜的是作者把javascript加密了,并且2011年以后就不再更新.如果项目中用到了新的jquery版本,甚至jbox就没 ...
- ACM-The Coco-Cola Store
题目: Once upon a time, there is a special coco-cola store. If you return three empty bottles to the s ...
- topcoder(BinaryCode)
Problem Statement Let's say you have a binary string such as the following: 011100011 One way t ...