[抄题]:

给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值。

MovingAverage m = new MovingAverage(3);
m.next(1) = 1 // 返回 1.00000
m.next(10) = (1 + 10) / 2 // 返回 5.50000
m.next(3) = (1 + 10 + 3) / 3 // 返回 4.66667
m.next(5) = (10 + 3 + 5) / 3 // 返回 6.00000

[暴力解法]:

来一个数就存数组,for 循环最近size个数求和取平均返回。

时间分析:size

空间分析:n

[思维问题]:

不知道为什么要定义全局变量:因为两个函数中都要用。

先提前声明,在函数中分别具体实现。

[一句话思路]:

先进先出,用queue实现就行。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 判断queue尺寸,已经满了,就先拿出来 再放进去。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 方法中只有实现,没有声明。que = new LinkedList<Integer>();即可

[复杂度]:Time complexity: O(n) Space complexity: O(n)

n个点,每个点执行一次。不知道queue都是这样吗?下次注意

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

前缀和,没有通用性,算了

  1. 时间分析:方便快速求a数组中某一段的和 前缀和做差法 a[k] + a[k + 1] +... + a[j] = s[j] - s[k -1] 时间复杂度降成o(1)

[Follow Up]:

[LC给出的题目变变变]:

239. Sliding Window Maximum median 一堆数求最值,用堆

773. Sliding Puzzle 最短路,用bfs

[代码风格] :

/  除号两边要打空格

public class MovingAverage {
/*
* @param size: An integer
*/
private double sum = 0;//
private int size;
private int val;
private Queue<Integer> que; public MovingAverage(int size) {
this.size = size;
que = new LinkedList<Integer>();
} /*
* @param val: An integer
* @return:
*/
public double next(int val) {
this.val = val;
this.sum = sum;
sum += val;
if (que.size() == size) {
sum = sum - que.poll();
}
que.offer(val); return sum / que.size();//
}
}

数据流滑动窗口平均值 · sliding window average from data stream的更多相关文章

  1. 滑动窗口(Sliding Window)技巧总结

    什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...

  2. [Swift]LeetCode239. 滑动窗口最大值 | Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  3. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

  4. Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解

    题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧 ...

  5. 346. Moving Average from Data Stream

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

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

  7. codevs4373 窗口==poj2823 Sliding Window

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53676   Accepted: 15399 ...

  8. [Swift]LeetCode346. 从数据流中移动平均值 $ 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 从数据流中移动平均值

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

随机推荐

  1. asp控制项目超时

    If Session("username")="" or isnull(Session("username")) Then %> &l ...

  2. linux进程与线程的区别【转】

    知乎上总结: "linux使用的1:1的线程模型,在内核中是不区分线程和进程的,都是可运行的任务而已.fork调用clone(最少的共享),pthread_create也是调用clone(最 ...

  3. ZooKeeper系列(3)命令操作 (转)

    原文地址:http://www.cnblogs.com/wuxl360/p/5817524.html 一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多 ...

  4. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  5. cs 更新

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...

  6. Web 跨域请求(OCRS) 前端解决方案

    1.同源策略如下: URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.j ...

  7. Executor框架(三)线程池详细介绍与ThreadPoolExecutor

    本文将介绍线程池的设计细节,这些细节与 ThreadPoolExecutor类的参数一一对应,所以,将直接通过此类介绍线程池. ThreadPoolExecutor类 简介   java.uitl.c ...

  8. Bogart gGrid.vb

    Namespace BogartMis.Cls Public Class gGrid '設定表格控的列標題的別名 '說明:strItem字符串的格式為"01,02,03,04,05" ...

  9. Linux编辑器|gedit|vi|vim编辑器

    gedit编辑器 gedit是一个Linux环境下的文本编辑器,类似windows下的写字板程序,在不需要特别复杂的编程环境下,作为基本的文本编辑器比较合适. sublime编辑器 Sublime T ...

  10. CUDA C Programming Guide 在线教程学习笔记 Part 4

    ▶ 图形互操作性,OpenGL 与 Direct3D 相关.(没学过,等待填坑) ▶ 版本号与计算能力 ● 计算能力(Compute Capability)表征了硬件规格,CUDA版本号表征了驱动接口 ...