/*
* 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. logging记录了其他操作的问题

    做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...

  2. LeetCode(205)Isomorphic Strings

    题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...

  3. 监控网络流量iftop和nethogs安装

    服务器环境是centos7,centos下通常使用iftop,或者nethogs来进行网络流量监控.这2个工具都需要先安装epel,因为这个库通常操作系统是不自带的.那么就先安装epel,使用的命令是 ...

  4. luogu3203 [HNOI2010]弹飞绵羊

    lct裸题 #include <iostream> #include <cstdio> using namespace std; int n, ski[200005], m, ...

  5. Selenium WebDriver- 指定页面加载时间

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  6. python 学习分享-购物车实操篇

    程序要求如下: '''购物车程序: 启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就 ...

  7. tensorflow——MNIST机器学习入门

    将这里的代码在项目中执行下载并安装数据集. 执行下面代码,训练.并评估模型: # _*_coding:utf-8_*_ import inputdata mnist = inputdata.read_ ...

  8. DB2 和 有道词典冲突: A communication error has been detected. Communication protocol being used: Reply.fill().

    我在本机安装了DB2 9.5. 使用java jdbc连接,一直没有问题. QC for db2 连接 也一直没有问题. 突然有一天 Java程序连接 报错: A communication erro ...

  9. BZOJ 4821 [Sdoi2017]相关分析 ——线段树

    打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...

  10. [暑假集训--数位dp]hdu5787 K-wolf Number

    Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation o ...