当前位置:网站首页>7-16 每日一题 剑指 Offer II 041. 滑动窗口的平均值
7-16 每日一题 剑指 Offer II 041. 滑动窗口的平均值
2022-07-16 10:05:00 【51CTO】
题目见: https://leetcode.cn/problems/qIsx9U/
给定一个整数数据流和一个窗口大小,根据该滑动窗口的大小,计算滑动窗口里所有数字的平均值。
实现 MovingAverage 类:
MovingAverage(int size) 用窗口大小 size 初始化对象。
double next(int val) 成员函数 next 每次调用的时候都会往滑动窗口增加一个整数,请计算并返回数据流中最后 size 个值的移动平均值,即滑动窗口里所有数字的平均值。
解一:简单直接,用collections.deque 双端队列维持一个滑动窗口。
仔细看一下,其中if len(self.queue) < self.size其实是没用的,可以优化掉。
解二:优化求和方式,用self.s 记录当前的和,每次滑动时只需在self.s上进行修改即可,不需要再对整个队列求和。
边栏推荐
- 现代化个人博客系统 ModStartBlog v5.3.0 留言接口新增,富文本升级
- Elk cluster deployment (IV) deployment logstash
- Logback different packages (business logs) are output to different log files
- Is the PMP certificate useful?
- 思科CCNP认证介绍
- 数据库与开源的未来
- 行政处罚法 例外规定
- cesium 区域裁剪、局部渲染
- 安装无线网卡驱动
- Advanced Mathematics -- Chapter 8 partial derivatives and total derivatives of implicit functions
猜你喜欢

极限实验室上新啦,期待已久的 INFINI Console 最新的 0.3 版本正式发布!

【古月21讲】ROS入门系列(3)——客户端Client、服务器Server的编程实现+自定义服务数据编程实现

Joint autoregressive and hierarchical priorities for learned image compression

Database daily question --- day 24: format department table

SNMP(简单网络管理协议)

Debugging of diagnostic function of analog power amplifier 75610

HCIP - PPP/HDLC与GRE/MGRE实验
![[tensorflow2.9] Titanic survival prediction - structured data modeling process](/img/66/55634b7559c051742bd21c722de7e0.png)
[tensorflow2.9] Titanic survival prediction - structured data modeling process

To achieve win-win results in team work efficiency and quality, this office collaboration tool is really enough!

AC management
随机推荐
Jitsi manu install (III)
AC management
Niuke - Huawei question bank (81~90)
JWT登录过期-自动刷新token方案介绍
[recognizing cloud Nativity] Chapter 4 cloud network section 4.9.4.2 - Implementation of smart network card
C serial port and TCP client sttech BytesIO
字符串和内存函数
Sydtek Internship (I): 4K and ble profile burning
Hcip - ppp/hdlc and gre/mgre experiments
Irregular area of OSPF
Redis基础课程讲义
Logback different packages (business logs) are output to different log files
English语法_定冠词the_小细节
Compressai: image compression framework based on pytorch
AP statistics
【生物信息学】外泌体miRNA成长训练营(14天)
实现团队工作效率与质量共赢,这款办公协同工具真够用!
LeetCode腾讯精选练习50题-059.螺旋矩阵II
[today's little go is going to throw away the garbage (1)]
Selenium八大元素定位及相对定位器