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. vlc 网页插件的 使用与控制 API http://www.xuebuyuan.com/2224602.html

    不知道vlc 是什么的请百度一下.. vlc 提供了ie浏览器的activeX插件和火狐或者chrome的插件,基本上覆盖了所有浏览器,所以有复杂解码需求的情况下用vlc来解决网页播放视频,也是一种没 ...

  2. eclipse 常用jar包总结

    BeanUtils: DbUtils: FileUpload: IO: Lang: Logging: cglib: mysql-connector: Pool:[datasource] DBCP:[d ...

  3. php history.back返回后表单数据丢失的解决办法

    js使用history.back返回表单数据丢失的主要原因就是使用了session_start();的原因,该函数会强制当前页面不被缓存.本文章向码农介绍php history.back返回后表单数据 ...

  4. Xshell批量导入IP地址

    我的xshell被覆盖了~~~结果原来的host没了,很郁闷要一个一个添加,网上找了很长时间在Xshell中批量添加IP的文章,结果都不行. 最后找到了https://blog.netsarang.c ...

  5. 编码风格和PEP8规范

    编码风格 错误认知 这很浪费时间 我是个艺术家 所有人都能穿的鞋不会合任何人的脚 我善长制定编码规范 正确认知 促进团队合作 减少bug处理 提高可读性,降低维护成本 有助于代码审查 养成习惯,有助于 ...

  6. uva-11205-枚举子集

    题意: 至少用多少列来表示输入中的二进制数,并且表示的数里面没有重复,最多P列,N个二进制数 所以......表示的最大二进制数是2^P,那么在2^P方内的数二进制最大值是P个1,最小是0,所以,枚举 ...

  7. OpenACC kernels

    ▶ 使用 kernels 导语并行化 for 循环 ● 一重循环 #include <stdio.h> #include <time.h> #include <opena ...

  8. 《内存数据库和mysql的同步机制》

    如下图  

  9. leetcode166

    public class Solution { public String fractionToDecimal(int numerator, int denominator) { HashMap< ...

  10. shiro 注解式前提

    <aop:config proxy-target-class="true"></aop:config> <bean class="org.a ...