Moving Average from Data Stream LT346
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
public class MovingAverage {
private Queue<Integer> queue;
private int capacity;
private double sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
queue = new ArrayDeque<>();
capacity = size;
sum = 0;
} public double next(int val) {
if(queue.size() == capacity) {
sum -= queue.poll();
}
queue.offer(val);
sum += val;
return sum/queue.size();
} public static void main(String[] args) {
int[] nums = {1, 10, 3, 5};
MovingAverage movingAverage = new MovingAverage(3);
for(int num: nums) {
System.out.println(movingAverage.next(num));
}
}
}
python:
import queue class MovingAverage:
def __init__(self, capacity):
self.capacity = capacity
self.sum = 0
self.window = queue.Queue(capacity) def next(self, num):
if self.window.full():
self.sum -= self.window.get() self.sum += num
self.window.put(num)
return self.sum/self.window.qsize() def test():
test_data = [1, 10, 3, 5]
test_subject = MovingAverage(3)
for num in test_data:
print (test_subject.next(num)) if __name__ == '__main__':
test()
Moving Average from Data Stream LT346的更多相关文章
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- 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 Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- 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数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- 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 ...
- [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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
随机推荐
- 1.5.4、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置自定义Java主目录位置)
配置自定义Java主目录位置 注意: Cloudera强烈建议安装JDK/ usr / java / jdk-version,允许Cloudera Manager自动检测并使用正确的JDK版本.如果在 ...
- CRM某些表加入审计
--更新参数文件,设置Audit等级 alter system set audit_trail=db,extended scope=spfile; --更新参数文件,开始Audit alter sys ...
- GsonFormat的使用 (转)
一.Android Studio快速添加Gson 具体操作: 1.File->Project Structure: 2.app->Dependencies->&qu ...
- as3.0复制影片简介(自我复制的三种形式)
//mc是被复制影片简介的实例名,(===在库中找到mc影片简介,右击“属性”,点击“为actionscript导出”,选中确定即可===这个是重点) var newSprite:Sprite=mc; ...
- Shell教程 之echo命令
1.显示普通字符串 这里的双引号完全可以省略,以下命令效果一致: echo "传递参数实例!" echo 传递参数实例! 2.显示转义字符 echo "\"传递 ...
- .py文件右键添加Edit with IDLE
1.打开注册表(regedit) 2.找到这个目录:HKEY_CLASSES_ROOT\SystemFileAssociations 3.找到.py的项,逐层新建 4.shell和edit,默认值改为 ...
- 【Linux 线程】常用线程函数复习《三》
1.关于函数pthraed_join与函数pthraed_detach 在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源 ...
- css控制div上浮下落
CSS3 示例:http://www.w3school.com.cn/cssref/pr_keyframes.asp 以下是代码: <!DOCTYPE html> <html> ...
- swift - 解析三方 - ObjectMapper
// // JYQueryBespeakModel.swift // rtb // // Created by chen on 2018/3/30 // 查询预约信息 import UIKit imp ...
- java_3选择与循环
1.三种执行顺序(流程控制语句) 在Java中,有三种执行结构,第一种:顺序结构.第二种:循环结构.第三种:选择结构. 2.顺序结构 自上而下,顺序执行. 3.循环结构 (1)while语句 初始化表 ...