【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 ...
随机推荐
- 【1】蛋白鉴定软件之X!Tandem
目录 1. 简介 2.下载安装 3. 软件试用 4. 结果 5. FAQ 1. 简介 X!Tandem是GPM:The Global Proteome Machine(主要基于Web的开源用户界面,用 ...
- C7的开机自启动设置
CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分 系统服务放在/usr/lib/systemd/system [Un ...
- Linux— file命令 用于辨识文件类型
Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...
- Linux升级命令yum upgrade和yum update的区别
Linux升级命令有两个分别是yum upgrade和yum update, 这个两个命令是有区别的: yum -y update 升级所有包同时也升级软件和系统内核 yum -y upgrade 只 ...
- springcloud - alibaba - 2 - 集成Feign - 更新完成
1.依赖 依赖管理 <parent> <artifactId>spring-boot-parent</artifactId> <groupId>org. ...
- A Child's History of England.30
CHAPTER 10 ENGLAND UNDER HENRY THE FIRST, CALLED FINE-SCHOLAR Fine-scholar, on hearing of the Red Ki ...
- oracle 日期语言格式化
TO_DATE ('17-JUN-87', 'dd-mm-yy', 'NLS_DATE_LANGUAGE = American')
- Linux:cp -rp
cp -rp[原文件或目录] [目标文件或目录] -r 复制目录 - p 保留文件属性 范例: cp -r /yy/k /yy/u /mm 复制目录u和目录k到目录mm中 cp -r /yy/ ...
- BeanDefinitionLoader spring Bean的加载器
spring 容器注册bean , 会把bean包装成beanDefinition 放进spring容器中,beanDefinitionLoader就是加载bean的类 . 一.源码 class Be ...
- MyBatis一对多映射简单查询案例(嵌套Mapper映射文件中的sql语句)
一.案例描述 书本类别表和书本信息表,查询书本类别表中的某一记录,连带查询出所有该类别书本的信息. 二.数据库表格 书本类别表(booktypeid,booktypename) 书本信息表(booki ...