/*
* 346. Moving Average from Data Stream
* 2016-7-11 by Mingyang
* 这里注意的就是(double) sum / count
* sum需要转换成double才能继续往下除,因为不转的话最后不能成为整数14除以3等于4
*/
class MovingAverage {
public Queue<Integer> queue;
public int sum = 0;
public int si;
public int count = 0; /** Initialize your data structure here. */
public MovingAverage(int size) {
this.si = size;
queue = new LinkedList<Integer>();
} public double next(int val) {
if (queue.size() < si) {
queue.add(val);
sum += val;
count++;
} else {
int temp = queue.poll();
sum -= temp;
queue.add(val);
sum += val;
}
return (double) sum / count;
}
}

346. Moving Average from Data Stream的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计

    [抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...

  4. [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 ...

  5. 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...

  6. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  7. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...

  2. ASP.NET Web网站中App_Code文件夹的作用及使用场景

    原文地址:Web Site项目和ASP.NET Web Application中App_Code文件夹的作用作者:宾的宾 我现在要建一个ASP.NET的网站了,不难吧,开始动手.如下图: 这种方法建立 ...

  3. hdu 6354

    Problem Description Edward is a worker for Aluminum Cyclic Machinery. His work is operating mechanic ...

  4. Linux学习-CentOS 7.x 预设启动的服务简易说明

    这里 仅介绍几个很常见的 daemons 而已,更多的信息呢,就得要麻烦你自己使用 systemctl list-unit-files --type=service 去查询.底下的建议主要是针对 Li ...

  5. Aizu 2450 Do use segment tree 树链剖分

    题意: 给出一棵\(n(1 \leq n \leq 200000)\)个节点的树,每个节点有一个权值. 然后有\(2\)种操作: \(1 \, a \, b \, c\):将路径\(a \to b\) ...

  6. Chromium Embedded Framework

    关于CEF 近期由于工作需要开始研究了Google的Chromium Embedded Framework(CEF),这是一个基于Google Chromium开源代码的项目,使用CEF可以很方便的在 ...

  7. jQuery 遍历函数 ,javascript中的each遍历

    jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合 ...

  8. rabbitmq exchange type

    This is the fourth installment to the series: RabbitMQ for Windows.  In thelast installment, we revi ...

  9. git命令综合

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势.Git常用操作命令:1) 远程仓库相关命令检出仓库:$ git clone git: ...

  10. hdoj--2082<母函数>

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=2082题目描述:26个字母各有价值,分别是1到26:给出每个字母的个数,求单词价值不超过50 的单词 ...