[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要分情况讨论除数,没有想数据结构

[一句话思路]:

用queue的动态长度避免分类讨论

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

[画图]:

[一刷]:

  1. queue刚满足等于时就应除,不够警惕,下次注意
  2. 动态计算时可以初始化为0,不能在方法中重置为0,下次注意

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

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

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

API design的题就是在类中声明引用,在方法中和实际的对象发生连接

  1. queue的实现(具体对象)是linkedlist,都是一条链,不难想象. queue用add也没事

[关键模板化代码]:

先说引用,再说对象

Queue<Integer> q;
int s;
double sum; public MovingAverage(int size) {
q = new LinkedList<Integer>();
s = size;
sum = 0;
}

[其他解法]:

[Follow Up]:

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

[代码风格] :

class MovingAverage {

    /** Initialize your data structure here. */
Queue<Integer> q;
int s;
double sum; public MovingAverage(int size) {
q = new LinkedList<Integer>();
s = size;
sum = 0;
} public double next(int val) {
//when the queue just equals the size, poll it out
if (q.size() == s) {
sum -= q.poll();
}
q.add(val);
sum += val;
return sum / q.size();
}
} /**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/

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. 346. Moving Average from Data Stream

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

  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. 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)

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

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

随机推荐

  1. java的try-with-resource机制

    在java7之后可以使用try(resource1, resource2){...}这样声明之后,在try{}执行完成之后或者抛异常跳出,都会调用reouce1.close(),resource2.c ...

  2. 什么是spark(六)Spark中的对象

    Spark中的对象 Spark的Conf,极简化的场景,可以设置一个空conf给sparkContext,在执行spark-submit的时候,系统会默认给sparkContext赋一个SparkCo ...

  3. wdlinux重新编译nginx

    背景: 由于web站点需要换主机,并且切换环境. 站点需要做反相代理(nginx),主机从美国迁移到香港[考虑到速度问题,为什么不用大陆阿里云ECS服务器?几十个域名备案麻烦,还有阿里云内地没有多ip ...

  4. Docker的一些常用

    日常使用的一些命令 1234567891011121314 docker pull mysql:tags // 拉mysql的tag版本 docker run -it -p(端口映射-主机端口:容器端 ...

  5. Codeforces 982C(dfs+思维)

    C. Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Cassandra Wiki Login JmxSecurity

    JmxSecurity 监控和管理Cassandra

  7. 019:InnoDB 表空间内部组织结构

    一. 表空间内部组织结构 表空间 内部有多个段对象(Segment) 组成 每个段(Segment)由区(Extent) 组成 每个区(Extent)由页(Page)组成 每个页里面报存数据 (或者叫 ...

  8. Java复习——I/O与序列化

    File类 java.io.File只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问,我们可以通过通过给其构造函数传一个路径来构建以文件,传入的路径名有一个小问题,就是Window ...

  9. 5月15日上课笔记-js中 location对象的属性、document对象、js内置对象、Date事件对象、

    location的属性: host: 返回当前主机名和端口号 定时函数: setTimeout( ) setInterval() 二.document对象 getElementById(); 根据ID ...

  10. Oracle参数修改是否需要重启等

    Oracle参数修改小结Oracle中有些参数是可以在session级别修改,有些则必须在system级别修改,有些参数不需要重启就能马上生效,有些参数必须重启才能生效,那么如何知道这些信息呢?可以从 ...