LeetCode Moving Average from Data Stream
原题链接在这里: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的更多相关文章
- [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 ...
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://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 ...
- [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 ...
随机推荐
- php接口
如果要继承多个类的方法规范,用接口,因为抽象类只能继承一个: 如果要共享一个方法体内容,用抽象类: <?php //接口是为了规范实现它的子类,以达到统一的目的 //接口不能被实例化 inter ...
- 《DSP using MATLAB》示例Example5.14
代码: x1 = [1,2,2]; x2 = [1,2,3,4]; y = circonvt(x1,x2,4) n1 = 0:1:length(x1)-1; n2 = 0:1:length(x2)-1 ...
- iOS深入学习(Block全面分析)
本文翻译自苹果的文档,有删减,也有添加自己的理解部分. 如果有Block语法不懂的,可以参考fuckingblocksyntax,里面对于Block 为了方便对比,下面的代码我假设是写在ViewCon ...
- 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。
找到该文件所在文件夹,右键属性,安全选项卡,添加-aspnet用户,并设置其权限为完全控制.如果还是不行,就添加一个Everyone用户并赋予完全控制权限windows server 2008中IIS ...
- java基础-继承
浏览以下内容前,请点击并阅读 声明 一个由其他类继承的类叫子类(也叫继承类,扩展类等),该类继承的类叫父类或超类.除了Object类意外,所有的类都有切仅有一个父类,如果一个类没有用extends关键 ...
- 【Cocos2d-x游戏开发】Cocos2d-x中的弱联网技术
在上一篇博客中,我们一起学习了如何在Cocos2d-x中存储数据和读取信息,本篇博客我们将一起讨论和数据存储同样重要的联网技术. 一.弱联网技术介绍 在网络游戏中许多重要的功能都需要网络连接,而根据需 ...
- NOI 题库 8465
8465 马走日 描述 马在中国象棋以日字形规则移动. 请编写一段程序,给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. ...
- [IOS初学]ios 第一篇 storyboard 与viewcontroller的关系
学习了一下ios,把一个基本的概念搞清楚了,在android或者wp中,大家基本都是习惯与一个画面场景代表一个类,新建场景的时候自动新建了类,但在ios中使用了storyboard之后发现,在stor ...
- scala shuffle
val arr = (0 to 100).map(_ * 1d) /// 下面这一步只能用to不能用until,scala里面实现返回的两个Range继承路径不同,不能混用 val a_shuffle ...
- 试验删除RAC(ORA10G)节点
1.环境概述 2.删除一个节点 本实验中以删除第二个节点为示例. 2.1删除数据库实例 在第一个节点上(存活的节点)上使用DBCA删除第二个节点的实例. 执行完以上操作可以看下crs_stat –t ...