当前位置:网站首页>剑指 Offer II 041. 滑动窗口的平均值
剑指 Offer II 041. 滑动窗口的平均值
2022-07-08 00:03:00 【小白码上飞】
一句话概要
使用队列实现,保证队列长度为滑动窗口的大小。即元素超出后从队列前移除。
题目
给定一个整数数据流和一个窗口大小,根据该滑动窗口的大小,计算滑动窗口里所有数字的平均值。
实现 MovingAverage 类:
MovingAverage(int size) 用窗口大小 size 初始化对象。
double next(int val) 成员函数 next 每次调用的时候都会往滑动窗口增加一个整数,请计算并返回数据流中最后 size 个值的移动平均值,即滑动窗口里所有数字的平均值。
链接:https://leetcode.cn/problems/qIsx9U
思路
就是说,窗口大小是固定的,当窗口里的元素个数不足窗口大小时,将整数加入窗口,并直接计算平均值。
如果如果新增整数后,个数超过窗口大小,则需要将最前面的元素剔除。
先进先出,那就是队列了。
解法:使用队列
代码
public class MovingAverage {
Queue<Integer> queue = new LinkedList<>();
int maxSize;
int size;
int total;
/** * Initialize your data structure here. */
public MovingAverage(int size) {
maxSize = size;
}
public double next(int val) {
// 当前值入队列
queue.add(val);
total += val;
if (size >= maxSize) {
Integer poll = queue.poll();
total = total - poll;
} else {
size++;
}
return (double) total / (double) size;
}
}
提交结果
扩展知识点:队列
常用api
队列,即先进先出。
Java中的队列,需要实现Queue
这个接口。
常用api的定义:
boolean offer(E e)
队尾添加元素。空间不够则返回false。E poll()
队头移除元素。如果队列为空,则返回null。E peek()
获取队头元素,不移除。如果队列为空,则返回null。boolean add(E e)
队尾添加元素。空间不够则抛出异常。E remove()
队头移除元素。如果队列为空,则抛出异常E element()
获取队头元素,不移除。如果队列为空,则抛出异常
我们经常使用到的实现,都是用LinkedList
来实现Queue
。而因为LinkedList
是个链表,因此不涉及到空间的限制,offer方法的实现本质上就是调用了add方法。
边栏推荐
- 快速熟知XML解析
- MATLAB R2021b 安装libsvm
- Gnuradio3.9.4 create OOT module instances
- GBASE观察 | 数据泄露频发 信息系统安全应如何守护
- Transportation, new infrastructure and smart highway
- Codeforces Round #649 (Div. 2)——A. XXXXX
- 2022 high voltage electrician examination skills and high voltage electrician reexamination examination
- Application of state mode in JSF source code
- 生态 | 湖仓一体的优选:GBase 8a MPP + XEOS
- Version 2.0 de tapdata, Open Source Live Data Platform est maintenant disponible
猜你喜欢
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
qt--将程序打包--不要安装qt-可以直接运行
Solve the error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
4、策略學習
2022 safety officer-c certificate examination paper and safety officer-c certificate simulated examination question bank
2022 examination for safety production management personnel of hazardous chemical production units and new version of examination questions for safety production management personnel of hazardous chem
The difference between distribution function and probability density function of random variables
随机推荐
写一个纯手写的qt的hello world
Measure the voltage with analog input (taking Arduino as an example, the range is about 1KV)
The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
2022 safety officer-b certificate examination question bank and safety officer-b certificate simulation test questions
npm 内部拆分模块
Version 2.0 de tapdata, Open Source Live Data Platform est maintenant disponible
如何让导电滑环信号更好
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
Anaconda3 tutorial on installing and adding Tsinghua image files
common commands
Matlab code on error analysis (MAE, MAPE, RMSE)
Matlab method is good~
Call (import) in Jupiter notebook ipynb . Py file
Gnuradio3.9.4 create OOT module instances
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
Redux usage
用户之声 | 对于GBase 8a数据库学习的感悟
图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂
Probability distribution
腾讯游戏客户端开发面试 (Unity + Cocos) 双重轰炸 社招6轮面试