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的更多相关文章

  1. 346. Moving Average from Data Stream

    /* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...

  2. LeetCode Moving Average from Data Stream

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. c#datagridview

    //保证显示当前活动单元格 this.Invoke(new Action(() => { dataGridView1.CurrentCell = dataGridView1.Rows[index ...

  2. jQuery 自定义插件 (分页控件)

    1.引入jqpage.js 2.html代码 <div id="page"> </div> 3.js 调用 $(function () { $.fn.jqp ...

  3. 修改hosts文件在本地使域名解析到指定IP

    # Additionally, comments (such as these) may be inserted on individual  # lines or following the mac ...

  4. linux 常见命令

    redhat 1.获取apt软件列表 sudo apt-cache search all 2.添加用户并创建目录 sudo useradd -m username -s /sbin/bash 3.vs ...

  5. Connect to Office365

    How to connect to office365 1. Connect to Exchange Online $UserCredential = Get-Credential $Session ...

  6. sql 触发器删除操作

    create trigger CheckDelete on 表 for delete as ) select @state=isnull(字段,'') from deleted if (@state& ...

  7. 冰冻三尺非一日之寒--web框架Django

    1.JS 正则    test   - 判断字符串是否符合规定的正则        rep = /\d+/;        rep.test("asdfoiklfasdf89asdfasdf ...

  8. JDBC判断数据库是否插入成功

    package com.xujianyou; import java.sql.*; public class TestConnect { public static void main(String ...

  9. html5+css+div随时笔记

    首先给头部文件引用格式 <link href="<%=basePath%>temp/public/css/style.css" rel="stylesh ...

  10. PHP 信号管理

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...