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

题目:

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已经等于size时若是继续加新的val就需要poll值.

Time Complexity: MovingAverage(size) O(1), next(val) O(1).

Space: O(size) 需要维护queue

AC Java:

 public class MovingAverage {

     private LinkedList<Integer> que;
private int size;
private int sum; /** Initialize your data structure here. */
public MovingAverage(int size) {
this.que = new LinkedList<Integer>();
this.size = size;
this.sum = 0;
} public double next(int val) {
if(que.size() == this.size){
this.sum -= que.poll();
}
que.offer(val);
sum += val;
return (double)this.sum/que.size();
}
} /**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/

LeetCode Moving Average from Data Stream的更多相关文章

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

  2. 346. Moving Average from Data Stream

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

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

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

  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. Intellij IDEA中的Mybatis Plugin破解

    具体的破解过程请看:https://github.com/luyanliang/profile/blob/master/idea/plugin/MybatisPlugin/Mybatis-Plugin ...

  2. Monitoring Processes with Supervisord

    If you're interested in more of this type of content, check out the Servers for Hackers eBook! As so ...

  3. Android之菜单总结

    在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 1. 选项菜单(OptionsMenu)详解 Activi ...

  4. iOS--UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】

    #import "ViewController.h" #define IMAGENUMBER 5 #define SIZE self.view.bounds.size @inter ...

  5. node模块函数图解

    已截图方式记录模块信息: HTTP模块: 对于网络返回处理状态封装了很多种,我已截图展现 以上状态也是在http协议中包含的状态. http函数: path模块:

  6. Leetcode Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. 【DP】HDU 1176

    HDU 1176 免费馅饼 题意:中文题目不解释. 思路:因为是从中间出发所以思路卡了许久,还在之前做了道HIHO入门的题.能想到的点,从时间思考,然后初始化1s的时候,4,5,6,的数值要特别赋值. ...

  8. 【ORACLE】特殊的NULL

    NULL 是数据库中特有的数据类型 Oracle 中对空的描述 nullAbsence of a value in a column of a row. Nulls indicate missing, ...

  9. 用户 'IIS APPPOOL\DefaultAppPool' 登录失败。

    今天新建了一个ASP.NET(Language=C#)网站,配置好数据库后编写了几行代码测试数据库的是否能正常使用. 当运行程序时,第一个页面都没有打开就出现了错误(因为我首页就访问数据库,填充一些D ...

  10. HTML常用属性

    blue:蓝色  red:红色  yellow:黄色  green:绿色  white:白色 gray:灰色 /*去掉下划线*/ text-decoration: none; /*添加下划线*/ te ...