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的更多相关文章

  1. 346. Moving Average from Data Stream

    /* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...

  2. LeetCode Moving Average from Data Stream

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

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

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...

  8. Moving Average from Data Stream

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

  9. [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 ...

随机推荐

  1. Python 3基础教程6-for循环语句

    本文介绍另外一种循环语句,for循环,直接看例子. 用for实现打印1到9的数字. 方法一:写入一个列表,然后遍历列表 # 这里介绍 for循环# 打印1到9 exampleList = [1,2,3 ...

  2. 孤荷凌寒自学python第九天Python的输出print的格式化

    孤荷凌寒自学python第九天Python的输出print的格式化 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (今天感觉手写笔记整得清楚些,汇总电子 笔记时,自己思路凌乱了,练习过程也还 ...

  3. leetcode 179. 最大数 解题报告

    给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说明: 输出结果 ...

  4. Android记事本10

    昨天: 从Activity中返回数据. 请求码和结果码的作用. 今天: Activity的启动模式. 遇到的问题: 无.

  5. java课后作业2017.10.20

    动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...

  6. ASP.NET Core MVC 运行所选代码生成器时出错

    添加Nuget Microsoft.VisualStudio.Web.CodeGeneration.Design Microsoft.EntityFrameworkCore.Tools

  7. 启动Tomcat时的常见问题及解决办法

    问题一:环境变量 1.检查jdk 验证jdk的配置,在运行-cmd中输入 java -version 即表示安装成功. 如果jdk没有问题,还需要配置两个环境变量.找到jdk和jre的路径,配置JAV ...

  8. ES6 学习体会

    第一部分: 1.初始化项目 npm init -y 2.安装ES6 环境 .babelrc 文件 babel-cli -g babel-ecmascript2015 babel-cli --save- ...

  9. 【bzoj3781】小B的询问 莫队算法

    原文地址:http://www.cnblogs.com/GXZlegend/p/6803821.html 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L ...

  10. 【bzoj4146】[AMPPZ2014]Divisors 数论

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801411.html 题目描述 给定一个序列a[1],a[2],...,a[n].求满足i!=j且a[i]|a[j] ...