当前位置:网站首页>剑指 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方法。
边栏推荐
- common commands
- 2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
- 2021 Shanghai safety officer C certificate examination registration and analysis of Shanghai safety officer C certificate search
- Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
- ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original
- 3、多智能体强化学习
- About snake equation (5)
- 2021 tea master (primary) examination materials and tea master (primary) simulation test questions
- Grey correlation analysis link (portal) matlab
- ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
猜你喜欢
break net
pb9.0 insert ole control 错误的修复工具
QT build with built-in application framework -- Hello World -- use min GW 32bit
4、策略學習
4、策略学习
2、TD+Learning
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
随机推荐
Problems of font legend and time scale display of MATLAB drawing coordinate axis
Gnuradio 3.9 using OOT custom module problem record
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Redis集群
Kafka connect synchronizes Kafka data to MySQL
Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method
Common fault analysis and Countermeasures of using MySQL in go language
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
break net
regular expression
如果时间是条河
能力贡献 GBASE三大解决方案入选“金融信创生态实验室-金融信创解决方案(第一批)”
ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
Gnuradio transmits video and displays it in real time using VLC
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
液压旋转接头的使用事项
4、策略學習
第七章 行为级建模
3. Multi agent reinforcement learning
MySQL数据库(2)