当前位置:网站首页>剑指 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方法。

边栏推荐
- Anaconda3 tutorial on installing and adding Tsinghua image files
- Gnuradio3.9.4 create OOT module instances
- 写一个纯手写的qt的hello world
- 2022 safety officer-b certificate examination question bank and safety officer-b certificate simulation test questions
- Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
- ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
- nacos-微服务网关Gateway组件 +Swagger2接口生成
- 从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
- Euler Lagrange equation
- 跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
猜你喜欢

2021-03-06 - play with the application of reflection in the framework

qt--将程序打包--不要安装qt-可以直接运行

Js中forEach map无法跳出循环问题以及forEach会不会修改原数组

Understanding of prior probability, posterior probability and Bayesian formula

MATLAB R2021b 安装libsvm

2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills

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

Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"

2022 safety officer-b certificate examination question bank and safety officer-b certificate simulation test questions

写一个纯手写的qt的hello world
随机推荐
2、TD+Learning
Redis master-slave replication
Write a pure handwritten QT Hello World
Plot function drawing of MATLAB
5. Contrôle discret et contrôle continu
Gnuradio 3.9 using OOT custom module problem record
ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
DataWorks值班表
4. Strategic Learning
2021-03-06 - play with the application of reflection in the framework
滑环在直驱电机转子的应用领域
2021-04-12 - new features lambda expression and function functional interface programming
生态 | 湖仓一体的优选:GBase 8a MPP + XEOS
The persistence mode of redis - RDB and AOF persistence mechanisms
common commands
Talk about smart Park
3、多智能体强化学习
Introduction to grpc for cloud native application development
Kafka-connect将Kafka数据同步到Mysql
npm 内部拆分模块