[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 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滑动窗口平均值的更多相关文章
- 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 ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- 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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- mysql主从复制——双机互为主从
第一.mysql主从复制(一主一从)怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下:首先需要做一些清理工作,如果之前配置了主从,但是配置失败了.结果会在/var/lib/mysql/ ...
- 2015ACM-ICPC长春E题(hdu5531)题解
一.题意 No response.T_T 二.思路 分$n$为奇数或者偶数讨论. 如果$n$是奇数,列出不等式组:$r_1+r_2=d_{1},r_2+r_3=d_{2},r_3+r_4=d_{3}, ...
- redis windows dll 下载
https://pecl.php.net/package/redis http://blog.csdn.net/leesin2011/article/details/72801629 http://w ...
- spark 多语言编程
参考官方地址:https://spark.apache.org/docs/1.6.2/programming-guide.html 误解: spark多语言的支持,并不是说spark可以操作各个语言写 ...
- 微软&中科大提出新型自动神经架构设计方法NAO
近期,来自微软和中国科学技术大学的刘铁岩等人发表论文,介绍了一种新型自动神经架构设计方法 NAO,该方法由三个部分组成:编码器.预测器和解码器.实验证明,该方法所发现的架构在 CIFAR-10 上的图 ...
- Android如何使用Https
什么是Https? HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...
- MongoDB集群与LBS应用系列(二)--与Hadoop集成
长期以来,我每开个系列,只有兴趣写一篇,很难持之与恒.为了克服这个长久以来的性格弱点,以及梳理工作半年的积累.最近一个月会写两篇关于Mongo在地理大数据方面的实践和应用,一篇关于推荐系统的初期准备过 ...
- jenkins API
1.curl http://199.168.299.99:8080/job/send_message/lastBuild/api/json --user administrator:1234 获取j ...
- Javascript,获取元素,write方法
一:Javascript:弱类型脚本语言,是一种动态类型.实现部分动画效果和用户交互等 -- html是骨架(页面结构) css样式 js是行为 -- 弱类型体现: JS代码可以写在body,he ...
- 《GPU高性能编程CUDA实战》附录一 高级原子操作
▶ 本章介绍了手动实现原子操作.重构了第五章向量点积的过程.核心是通过定义结构Lock及其运算,实现锁定,读写,解锁的过程. ● 章节代码 #include <stdio.h> #incl ...