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 ...
随机推荐
- 系统吞吐量(TPS)、用户并发量
PS:下面是性能测试的主要概念和计算公式,记录下: 一.系统吞度量要素: 一个系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联. 单个reqeust 对CPU消耗越高 ...
- Java(Helloworld.java)
public class A{ public static void main(String args[]){ System.out.println("Hello world!") ...
- Socket 类通信例子-第24章
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- python操作mongodb数据库
一.MongoDB 数据库操作 连接数据库 import pymongo conn = pymongo.Connection() # 连接本机数据库 conn = pymongo.Connection ...
- JS:事件处理程序
在JQuery中有个toggle事件,可以绑定两个或多个函数,可以轮流相应click事件,这两天学习到原来javascript中有两个方法(也可以说是四个)同样可以实现这个功能. #box{ marg ...
- CSS:文字不在图片中间
平时用的text-align属性比较多,相比较而言vertical-align则用的比较少. 当文字和图片布局在一起时,文字不能对齐到图片的中间,向这样: HTML: <div><i ...
- Tornado学习笔记12 tornado.httpserver-.非阻塞的Http服务器
是一个非阻塞的,单线程的Http 服务器. 一般地,应用程序很少与HttpServer类直接交互,除非在进程开始时启动服务时(甚至在使用tornado.web.Applicaiton.listen时也 ...
- C#_Express-ickd接口
爱查快递接口使用 using System; using System.Collections.Generic; using System.IO; using System.Net; using Sy ...
- CSS常用属性
//边界线 border: 1px solid #E4E4E4; //绝对 定位 position: absolute; //相对定位 position: relative; //超出部分隐藏 ove ...
- Django model字段类型清单
转载:<Django model字段类型清单> Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField ...