当前位置:网站首页>剑指 Offer II 041. 滑动窗口的平均值
剑指 Offer II 041. 滑动窗口的平均值
2022-07-17 13:33:00 【Sasakihaise_】

【分析】维护一个长度为size的双端队列,在没有达到size大小前一直往里面加元素即可,当达到size大小后,就去掉队首在队尾加入新来的元素即可。
class MovingAverage {
// 滑动窗口 10:10 10
int n;
int t = 0;
double sum = 0;
Deque<Integer> queue = new LinkedList();
/** Initialize your data structure here. */
public MovingAverage(int size) {
n = size;
}
public double next(int val) {
if (t < n) {
t++;
sum += val;
queue.offer(val);
return sum / t;
}
sum -= queue.poll();
queue.offer(val);
sum += val;
return sum / n;
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/前几天把jio扭了,所以摆了几天,现在开始把之前欠的债补上。
边栏推荐
- 空天地海一体化网络体系架构与网络切片技术
- Summary of port mirroring methods with VDS or NSX under vSphere
- 金鱼哥RHCA回忆录:CL210描述OPENSTACK控制平面--识别overclound控制平台服务+章节实验
- vSphere 下借助 vDS 或 NSX 做端口镜像的方法总结
- openfoam热流边界条件
- Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
- Google Earth engine app (GEE) - set up a nighttime lighting timing analysis app in China
- C serialport configuration and attribute understanding
- Opencv programming: opencv3 X trains its own classifier
- 反向散射通信的未来应用与技术挑战
猜你喜欢

Know what it is, and know why, JS object creation and inheritance

Use testeract JS offline recognition picture text record

Tencent cloud server uses image to deploy WordPress personal website!

NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA

Game theory (Depu) and investment (40/100)

How much money can you make by inventing flash memory? This is a Japanese dog blood story

ROS 重名

(1) Learn about MySQL
![[leetcode weekly replay] 302 weekly 20220717](/img/38/446db9b4755f8b30f9887faede7e95.png)
[leetcode weekly replay] 302 weekly 20220717

人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
随机推荐
NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
Brush questions with binary tree (2)
高数__方程与函数的关系
企业远程办公如何更高效?
Game theory (Depu) and investment (40/100)
2022/7/15
Google Earth Engine APP(GEE)—设定中国区域的一个夜间灯光时序分析app
Pytorch框架 学习记录1 CIFAR-10分类
[csp-j 2021] summary
The difference between journal log and oplog log
Connected graph (union search set)
String类型函数传递问题
LeetCode 2325. 解密消息(map)
[handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
Pytorch.nn实现多层感知机
mysql 查询报错
【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
【设计过程】.NET ORM FreeSql WhereDynamicFilter 动态表格查询功能
LeetCode 2335. Minimum total time required to fill the cup
vulnhub inclusiveness: 1