346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]:
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的动态长度避免分类讨论
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- queue刚满足等于时就应除,不够警惕,下次注意
- 动态计算时可以初始化为0,不能在方法中重置为0,下次注意
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
API design的题就是在类中声明引用,在方法中和实际的对象发生连接
- 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数据窗口流中位数的数据结构设计的更多相关文章
- [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 ...
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- 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 ...
- 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
- [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 ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- [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 ...
- 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 ...
随机推荐
- python编程规范系列--建议08~18
本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本章主要内容 建议8:利用assert语句来发现问题 建议9:数据交换值时不推荐使用中间交换变量 建议10 ...
- 【转】Linux动态链接(4)ldd与ldconfig
原文网址:http://tsecer.blog.163.com/blog/static/15018172012414105551345/ 一.动态链接工具ldd和ldconfig是动态链接的两个重要辅 ...
- CSS基本样式简单介绍
具体详情内容请查阅<css参考手册> 一.基本结构样式 width 宽度 height 高度 background 背景 border 边框 padding 内边距 margin 外边距 ...
- 16.Python使用lxml爬虫
1.lxml是解析库,使用时需要导入该包,直接在命令行输入:pip3 install lxml,基本上会报错.正确应该去对应的网址:https://pypi.org/project/lxml/#fil ...
- 自定义显示提示一段时间自动消失ShowMsgText控件
public partial class ShowMsgText : Label { public string TextMsg { get { return Text; } set { timer1 ...
- java 类加载器体系结构
- python中format函数学习笔记
简而言之,format函数就是用{}来代替之前的输出字符时使用的% print('my name is %s and I am %d years old' % ('porsche',23)) 下面详细 ...
- 自增自减 a++,++a,a--,--a
1.自增(++)自减(--)运算符是一种特殊的算术运算符,在算术运算符中需要两个操作数来进行运算,而自增自减运算符是一个操作数. 实例: public class selfAddMinus{ publ ...
- GC之九--gc调优
目标 满足应用的响应时间和吞吐量需求,尽量减少GC对应用的影响 原则 大部分时候都不需要调优GC,只需配置-Xms,-Xmx即可,JVM会自动进行调整 先满足响应时间需求,再满足吞吐量需求 FullG ...
- web 服务基础
用户通过网站访问浏览器都发生了什么 如图,用户请求www.joker.com发生 1. 用户访问网站流程框架2. dns解析原理3. tcp/ip三次握手过程原理4. http协议原理(www服务的请 ...