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
题目标签:Design
这道题目让我们设计一个移动平均值的结构,我们有一个input size, 这个size是控制着我们的window。每次都新的数字进来,如果目前的size小于window,那么继续加入。如果新的数字进来,size已经满了,等于window size。那么我们需要把第一个数字去除,然后加入新的数字。可以利用ArrayList来模仿queue实现,add 加入到最后, remove(0) 把第一个数字去除。还要设一个sum, 每次加入,就加入sum, 当满了之后,每次去除,只要从sum里减去。这样就可以避免每一次加入一个数字的时候,都要遍历一次queue来得到所有数字之和。
Java Solution:
Runtime beats 71.48%
完成日期:07/09/2017
关键词:Design
关键点:利用ArrayList来模仿Queue
public class MovingAverage {
ArrayList<Integer> queue;
int queue_size;
double sum;
/** Initialize your data structure here. */
public MovingAverage(int size)
{
queue = new ArrayList<>(size);
queue_size = size;
sum = 0;
}
public double next(int val)
{
if(queue.size() == queue_size) // meaning it is full
{
sum -= queue.get(0); // minus head
queue.remove(0); // remove the head
}
queue.add(val); //append the new integer
sum += val; // add into sum
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);
*/
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
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 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- 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 ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
随机推荐
- [01] Servlet是什么
1.Servlet是什么 Servlet(Server Applet),全称Java Servlet,是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. 1.1 ...
- IT连创业系列:创业者逆境下的思维
距上篇文章,又半个多月过去了,是时候来一发阶段性的总结了. 可能最近比较懒,也可能是想不到写文的主题,故写文已变成越来越艰难的一个任务. 这个系列的大标题,也改了:它从<一个想法>到< ...
- 【京东详情页】——原生js学习之匿名函数
一.引言 在js模块中,要给每一个功能封装一个匿名函数.为了更好的理解什么是匿名函数,为什么要用匿名函数,我做了一些查阅和学习. 二.匿名函数 什么是:在创建时,不被任何变量引用的函数. 为什么:节约 ...
- oracle 数据字典和动态性能视图
一.概念数据字典是oracle数据库中最重要的组成部分,它提供了数据库的一些系统信息.动态性能视图记载了例程启动后的相关信息. 二.数据字典1).数据字典记录了数据库的系统信息,它是只读表和视图的集合 ...
- JAVA 并发(待补全!)
从性能上看 如果没有任务会阻塞 那么在单处理器的机器人使用并发就没有任何意义 (需要上下文切换 时间反而长) 进程是运行在他自己地址空间的自包容的程序 协作多线程与抢占式多线程 想要定义任务需要实现R ...
- Longge's problem poj2480 欧拉函数,gcd
Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6918 Accepted: 2234 ...
- HDU 1754 I Hate It(线段树区间求最值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- C# 如何添加Word文本和图片超链接
超链接简单来讲就是内容链接,通过设置超链接可以实现对象与网页.站点之间的连接.链接目标可以是网页.图片.邮件地址.文件夹或者是应用程序.设置链接的对象可以是文本或者图片. 在以下内容中,我将介绍如何用 ...
- webpack2使用ch7-loader解析css 自动添加浏览器前缀
1 目录结构 安装的依赖 "autoprefixer": "^6.7.7", "css-loader": "^0.28.0&qu ...
- SAP 图标查找及方法
1. 图标查找 方法一:通过TCODE查找图标对应的图标名称 执行TCODE:ICON 查找图标对应的图标名称 方法二:通过方法一查出图标名称查找对应的图标ID SE11类型池根据方法一查找的ICON ...