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 ...
 
随机推荐
- WebApp  九宫格抽奖简易demo
			
代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...
 - #define与typedef在重定义类型中的区别
			
#define 为完全的替换 typedef 重新定一个数据类型 eg #define charp1 char* typedef char* charp2charp1 a,b; //a char* b ...
 - UICollectionViewCell 网格显示数据
			
using System; using System.Collections.Generic; using Foundation; using UIKit; namespace ddd { publi ...
 - 他(he)(钟神)
			
他[问题描述]一张长度为N的纸带,我们可以从左至右编号为0 −N(纸带最左端标号为0) .现在有M次操作,每次将纸带沿着某个位置进行折叠,问所有操作之后纸带的长度是多少.[输入格式]第一行两个数字N, ...
 - javaweb 基于java Servlet登入 简单入门案例
			
项目流程 第一步:创建一个java webproject第二步:创建三个界面,1,login.jsp 2 success.jsp 3 fail.jsp第三步:更改新建界面编码格式,utf-8 默然编码 ...
 - .NET FRAMEWORK版本:4.0.30319; ASP.NET版本:4.6.118.0
			
https://gqqnbig.me/2015/11/23/net-framework%e7%89%88%e6%9c%ac4-0-30319-asp-net%e7%89%88%e6%9c%ac4-6- ...
 - maven报brors occurred during the build
			
原因分析: 此问题一般发生在eclipse保存文件并自动部署时候.本人在写项目的时候,还没等部署好,关闭了了eclipse,结果出现了这种情况.有一种产生此错误的原因是因为此项目不不是由eclipse ...
 - JAVA中ListIterator和Iterator详解与辨析
			
在使用Java集 合的时候,都需要使用Iterator.但是java集合中还有一个迭代器ListIterator,在使用List.ArrayList. LinkedList和Vector的时候可以使用 ...
 - 用Redis存储session
			
0.什么是Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API ---维基百科 1.与其他用户状态保存方 ...
 - 微软压力测试工具 web application stress
			
转自 http://www.cnblogs.com/tonykan/p/3514749.html lbimba 铜牌会员 这里给广大的煤油推荐一个web网站压力测试工具.它可以用来模拟多个用户操作网 ...