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. CentOS 6.6下安装配置Tomcat环境

    本文转载至:http://www.linuxidc.com/Linux/2015-08/122234.htm 实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验 ...

  2. 通过优化在UE4中实现良好性能和高质量视觉效果

    转自:http://gad.qq.com/program/translateview/7160166 译者:赵菁菁(轩语轩缘)  审校:李笑达(DDBC4747) 对于任何追求UE4性能最佳.同时又想 ...

  3. matlab批量修改图片大小

    转自:http://blog.csdn.net/cike0cop/article/details/53087995 %author:coplin %time:2016-10-10 %function: ...

  4. 代码生成器 CodeSmith 的使用(二)

    在第一篇中,简单的介绍了 CodeSmith 的使用方法,这次做一个生成简单的数据库字段属性的模板.以下只粘贴主要的代码片段. <%-- Name: Copyright © Sun 2013-2 ...

  5. selenium+python自动化98--文件下载弹窗处理(PyKeyboard)

    前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...

  6. leetcode500

    public class Solution { public string[] FindWords(string[] words) { var list1 = new List<char> ...

  7. shiro 注解式前提

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

  8. xe DateTimePicker.Date bug

    xe6 bug xe7 ok DateTimePicker1->DateTime.DateString(); DateTimePicker1->DateTime.DateTimeStrin ...

  9. XE 创建 Active Form

    XE6: http://docwiki.embarcadero.com/RADStudio/XE6/en/Generating_an_Active_Form_Based_on_a_VCL_Form h ...

  10. PHP图像 因其本身有错无法显示

    昨天终于将客户的一个网站迁移至虚拟主机上,满怀希望的敲入网址.唰的一声,网站很轻松的被打开了. 心里那个高兴啊~~~ 咦,怎么产品图片都没有显示出来.一块块都是空白.敲入img src对应的地址,看看 ...