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先进先出的特点即可。
public class MovingAverage {
Queue<Integer> q;
double sum = ;
int size;
/** Initialize your data structure here. */
public MovingAverage(int s) {
q = new LinkedList();
size = s;
}
public double next(int val) {
if (q.size() == size) {
sum = sum - q.poll();
}
q.offer(val);
sum += val;
return sum / q.size();
}
}
Moving Average from Data Stream的更多相关文章
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- 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 LT346
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 ...
- 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 从数据流中移动平均值
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 ...
随机推荐
- 华为5G空口新技术(2015年)
2015-03-24 长江后浪推前浪,4G建设方兴未艾,业界关于5G的讨论已如火如荼.对于每一代移动通信,空口技术都相当于王冠上的明珠. 在月初的世界移动通信大会上,华为发布了面向5G的新空口,并展出 ...
- Jenkins + svn + maven 构建持续集成环境搭建
Jenkins简介 Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: ...
- Android组件化和插件化开发
http://www.cnblogs.com/android-blogs/p/5703355.html 什么是组件化和插件化? 组件化开发就是将一个app分成多个模块,每个模块都是一个组件(Modul ...
- 北京培训记day2
后缀三姐妹 P.S.后缀大家族关系:后缀自动机fail指针=后缀树,后缀树前序遍历=后缀数组 一.后缀数组:orz罗穗骞集训队论文 给每个后缀按字典序排序 rank[]表示从i开始的后缀排名多少 sa ...
- Downgrade PHP 7 to PHP 5.6 on Ubuntu 16.04
Downgrade PHP 7 to PHP 5.6 on Ubuntu ubuntu16.04 系统源自带是7.0的,如何降级安装PHP 5.6呢 .? apt-get install -y lan ...
- 修改github.com域名解析
http://ping.chinaz.com/ 首先在这个网站查询 github.com 然后选择ping速度最好的IP地址 将其填充到hosts文件中.win7路径:C:\Windows\Syst ...
- 移动端 Web 开发前端知识整理
文章来源: http://www.restran.net/2015/05/14/mobile-web-front-end-collections/ 最近整理的移动端 Web 开发前端知识,不定期更新. ...
- Nginx 和 IIS 实现动静分离
前段时间,搞Nginx+IIS的负载均衡,想了解的朋友,可以看这篇文章:<nginx 和 IIS 实现负载均衡>,然后也就顺便研究了Nginx + IIS 实现动静分离.所以,一起总结出来 ...
- nginx问题收集
1.问题: 用户在微信授权成功之后, 跳转到网页时, 出现nginx的错误界面, 复制当前这个链接在网页中打开则正常显示, 同时nginx日志文件中报错(upstream sent too big ...
- 更换域名后的数据库sql的执行命令
原来域名为trz.lqzcw.com 更改成 www.trzbearing.com UPDATE wp_options SET option_value = replace(option_value, ...