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

若当前queue的size大于sliding window的长度

则从queue中remove一个元素

注意:

全局变量用下划线_size, _sum是个好的coding style

代码:

 class MovingAverage {
Queue<Integer> _queue = new LinkedList<>();
int _size;
double _sum; /** Initialize your data structure here. */
public MovingAverage(int size) {
_size = size;
_sum = 0.0;
} public double next(int val) {
_queue.add(val);
_sum = _sum + val;
if(_queue.size() > _size ){
_sum = _sum - _queue.remove();
}
return _sum/_queue.size();
}
} /**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/

[leetcode]346. Moving Average from Data Stream滑动窗口平均值的更多相关文章

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

  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. 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计

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

  4. 346. Moving Average from Data Stream

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

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

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

  6. LeetCode Moving Average from Data Stream

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

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

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

  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. autocomplete 自动填充 combobox

    目录(?)[-] autocomplete有两种 一种 是 jquery ui里的 autocomplete httpjqueryuicomautocomplete 另一种是 ASPNET AJAX ...

  2. Windows Event 事件

    事件对象就像一个开关:它只有两种状态(开和关). 开状态:我们称其为“有信号” 关状态:我们称其为“无信号” 可以在一个线程的执行函数中创建一个事件对象,然后观察它的状态,如果是“无信号”就让该线程睡 ...

  3. Map 合并

    比如说 qq.com 100 163.com  90 QQ.COM 10 Qq.Com  5 …… 如果统计的话,需要忽略大小写的,即 QQ邮箱总共是100+10+5,怎么写? 其实这个应该不难的,就 ...

  4. python之路day03

    1  复习计算机基础 计算机基础我们讲到完整的计算机系统包括了:应用程序,操作系统,硬件三部分.那么硬件又分为:cpu,内,和硬盘. 对于用户来说我们操作计算机是通过应用程序来间接控制计算机.当我们打 ...

  5. SpringJdbc之queryForXXX大全解读

      一.查询单个字段  Object queryForObject(String sql, Object[] args, Class requiredType)  其中Class requiredTy ...

  6. 大话java性能优化 pdf 下载(全本)

    扫加公众号,回复”大话java性能优化",免费获取此书.

  7. flask框架预备知识

    1.web预备知识 2.flask介绍 3.web框架的本质及分类 4.flask安装与基本设置 1.web预备知识 HTTP协议:https://www.cnblogs.com/wyb666/p/9 ...

  8. Coxph model Pvalue Select

    I am calculating cox propotional hazards models with the coxph function from the survival package.   ...

  9. selenium+python自动化89-用例不通过的时候发送邮件

    前言 实现需求:当测试用例全部通过的时候,不发邮件,当用例出现Error或Failure的时候发送邮件 解决思路:生成html测试报告后,用bs4解析html页面,写个函数判断页面上是都有不通过的记录 ...

  10. Jquery学习(二)

    滚轮事件与函数节流 jquery.mousewheel插件使用 jquery中没有鼠标滚轮事件,原生js中的鼠标滚轮事件不兼容,可以使用jquery的滚轮事件插件jquery.mousewheel.j ...