【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/moving-average-from-data-stream/
题目描述
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3
题目大意
计算一个固定大小的滑动窗口中的平均数。
解题方法
队列
一个固定大小的队列,每次插入数据之前判断队列是否已满,删除队列的头部元素,在队列的尾部插入元素即可。由于每次都求和比较耗时,因此可以使用一个sum变量保存队列里面元素的和。
C++代码如下:
class MovingAverage {
public:
/** Initialize your data structure here. */
MovingAverage(int size) {
cap = size;
}
double next(int val) {
if (que.size() >= cap) {
sum -= que.front();
que.pop_front();
}
que.push_back(val);
sum += val;
return (double) sum / que.size();
}
private:
deque<int> que;
int cap = 0;
int count = 0;
long long sum = 0;
};
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage* obj = new MovingAverage(size);
* double param_1 = obj->next(val);
*/
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】346. Moving Average from Data Stream 解题报告(C++)的更多相关文章
- LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)$
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- [leetcode]346. Moving Average from Data Stream滑动窗口平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- [LeetCode] Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- Moving Average from Data Stream -- LeetCode
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
随机推荐
- R 语言实战-Part 5-2笔记
R 语言实战(第二版) part 5-2 技能拓展 ----------第21章创建包-------------------------- #包是一套函数.文档和数据的合集,以一种标准的格式保存 #1 ...
- MAFFT 进行多序列比对
简介 最经典和广为熟知的多序列比对软件是 clustalw . 但是现有的多序列比对软件较多,有文献报道:比对速度(Muscle>MAFFT>ClustalW>T-Coffee),比 ...
- oracle 将电话号码中间4位数以星号*代替
select replace('17665312355',substr('17665312355',4,4),'****') as phone, #类似E ...
- flink-----实时项目---day05-------1. ProcessFunction 2. apply对窗口进行全量聚合 3使用aggregate方法实现增量聚合 4.使用ProcessFunction结合定时器实现排序
1. ProcessFunction ProcessFunction是一个低级的流处理操作,可以访问所有(非循环)流应用程序的基本构建块: event(流元素) state(容错,一致性,只能在Key ...
- 乱序拼图验证的识别并还原-puzzle-captcha
一.前言 乱序拼图验证是一种较少见的验证码防御,市面上更多的是拖动滑块,被完美攻克的有不少,都在行为轨迹上下足了功夫,本文不讨论轨迹模拟范畴,就只针对拼图还原进行研究. 找一个市面比较普及的顶像乱序拼 ...
- File类及常用操作方法
import java.io.File; import java.io.IOException; public class file { public static void main(String[ ...
- html href页面跳转获取参数
//传递参数 var id = columnData.id; var companyname = encodeURI(columnData.companyname); var linename = e ...
- 【Java 调优】Java性能优化
Java性能优化的50个细节(珍藏版) 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: ...
- SpringBoot的定时任务
springBoot定时任务可分为多线程和单线程,而单线程又分为注解形式,接口形式 1.基于注解形式 基于注解@Scheduled默认为单线程,开启多个任务时,任务的执行时机会受上一个任务执行时间的影 ...
- CSS font-size: 0去除内联元素空白间隙
我们在编写HTML标签的时候,通常会使用换行,缩进来保证代码的可读性.同时,在编写CSS样式的时候,也会需要把一些元素设置为inline或inline-block.这样一来,有时在页面中会出现意外的空 ...