Moving Average from Data Stream

时间:2023-01-29 21:54:53

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

分析:

利用Queue先进先出的特点即可。

 public class MovingAverage {
Queue<Integer> q;
double sum = ;
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. 346&period; 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&period; Moving Average from Data Stream (数据流动中的移动平均值)&dollar;

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

  4. Moving Average from Data Stream LT346

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

  5. &lbrack;leetcode&rsqb;346&period; 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. 346&period; Moving Average from Data Stream数据窗口流中位数的数据结构设计

    [抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...

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

  8. &lbrack;LeetCode&rsqb; 346&period; 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. 【LeetCode】346&period; Moving Average from Data Stream 解题报告&lpar;C&plus;&plus;&rpar;

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

随机推荐

  1. 密码学初级教程&lpar;五)消息认证码MAC-Message Authentication Code

    密码学家工具箱中的6个重要的工具: 对称密码 公钥密码 单向散列函数 消息认证码 数字签名 伪随机数生成器 MAC能识别出篡改和伪装,也就是既可以确认消息的完整性,也可以进行认证. 消息认证码的输入包 ...

  2. JavaScript 内部对象

    欢迎访问我的个人博客:http://blog.cdswyda.com/ 原文文地址:JavaScript 内部对象

  3. zoj 1622 Switch 开关灯 简单枚举

    ZOJ Problem Set - 1622 Switch Time Limit: 2 Seconds      Memory Limit: 65536 KB There are N lights i ...

  4. 用CSS样式画横线和竖线的方法

    今天在做网页的时候,需要用到CSS画横线,虽然比较简单,但也出了一些小问题,拿来做个备忘. 方法一:用DIV,代码如下:(推荐此方法)     <div style="width:80 ...

  5. hibernate它5&period;many2one单向

    关系数据库表之间的关系: 1 正确 1 1 正确 许多 许多 正确 许多 表间关系设计 基于主键关联 基于外键关联 基于中间表 1 对 1关系实现: 基于主键关联 基于外键关联 基于中间表 1 对 多 ...

  6. Fine Uploader 简单配置方法

    由于jquery.uploadify是基于flash的jquery上传控件,客户老是说出问题,所以今天换成了一个纯js的异步上传控件. 这方面的资料很少,故此记下来分享一下. 项目地址:Fine Up ...

  7. web&period;xml is missing and &lt&semi;failOnMissingWebXml&gt&semi; is set to true

    这时候需要右击项目-->Java EE Tools-->Generate Deployment Descriptor Stub .然后系统会在src/main/webapp/WEB_INF ...

  8. 18&period;12&period;09-C语言练习:黑洞数 &sol; Kaprekar问题

    题目: 程序: #include <stdio.h> int main(void) { int n, a, b, c, t, A, B; printf("输入一个三位数整数:&q ...

  9. Android string资源 包含 数学符号等特殊字符 及 参数占位符

    定义:<?xml version="1.0" encoding="utf-8"?><resources>    <string n ...

  10. ajax文件上传-FormData&lpar;&rpar;

    HTML: <form action=""> <input type="file" id="file1" name=&qu ...