C++
Java
Python
Python3
C
C#
JavaScript
TypeScript
PHP
Swift
Kotlin
Dart
Go
Ruby
Scala
Rust
Racket
Erlang
Elixir
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class MovingAverage {
public:
MovingAverage(int size) {
}
double next(int val) {
}
};
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage* obj = new MovingAverage(size);
* double param_1 = obj->next(val);
*/
运行代码
提交
javascript 解法, 执行用时: 96 ms, 内存消耗: 47.3 MB, 提交时间: 2023-10-15 18:38:09
/**
* @param {number} size
*/
var MovingAverage = function(size) {
this.queue = [];
this.size = size;
this.sum = 0;
};
/**
* @param {number} val
* @return {number}
*/
MovingAverage.prototype.next = function(val) {
if (this.queue.length === this.size) {
this.sum -= this.queue.shift();
}
this.queue.push(val);
this.sum += val;
return this.sum / this.queue.length;
};
/**
* Your MovingAverage object will be instantiated and called as such:
* var obj = new MovingAverage(size)
* var param_1 = obj.next(val)
*/
golang 解法, 执行用时: 16 ms, 内存消耗: 6.7 MB, 提交时间: 2023-10-15 18:37:38
type MovingAverage struct {
size, sum int
q []int
}
func Constructor(size int) MovingAverage {
return MovingAverage{size: size}
}
func (m *MovingAverage) Next(val int) float64 {
if len(m.q) == m.size {
m.sum -= m.q[0]
m.q = m.q[1:]
}
m.sum += val
m.q = append(m.q, val)
return float64(m.sum) / float64(len(m.q))
}
/**
* Your MovingAverage object will be instantiated and called as such:
* obj := Constructor(size);
* param_1 := obj.Next(val);
*/
java 解法, 执行用时: 37 ms, 内存消耗: 47 MB, 提交时间: 2023-10-15 18:37:21
class MovingAverage {
Queue<Integer> queue;
int size;
double sum;
public MovingAverage(int size) {
queue = new ArrayDeque<Integer>();
this.size = size;
sum = 0;
}
public double next(int val) {
if (queue.size() == size) {
sum -= queue.poll();
}
queue.offer(val);
sum += val;
return sum / queue.size();
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/
cpp 解法, 执行用时: 20 ms, 内存消耗: 14 MB, 提交时间: 2023-10-15 18:37:05
class MovingAverage {
public:
MovingAverage(int size) {
this->size = size;
this->sum = 0.0;
}
double next(int val) {
if (qu.size() == size) {
sum -= qu.front();
qu.pop();
}
qu.emplace(val);
sum += val;
return sum / qu.size();
}
private:
int size;
double sum;
queue<int> qu;
};
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage* obj = new MovingAverage(size);
* double param_1 = obj->next(val);
*/
python3 解法, 执行用时: 68 ms, 内存消耗: 19.5 MB, 提交时间: 2023-10-15 18:36:51
class MovingAverage:
def __init__(self, size: int):
self.size = size
self.sum = 0
self.q = deque()
def next(self, val: int) -> float:
if len(self.q) == self.size:
self.sum -= self.q.popleft()
self.sum += val
self.q.append(val)
return self.sum / len(self.q)
# Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)