-
Notifications
You must be signed in to change notification settings - Fork 2
/
qIsx9U.rs
45 lines (37 loc) · 952 Bytes
/
qIsx9U.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
struct MovingAverage {
v: Vec<f64>,
len: f64,
index: usize,
sum: f64,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MovingAverage {
/** Initialize your data structure here. */
fn new(size: i32) -> Self {
Self {
v: vec![0f64; size as usize],
len: 0f64,
index: 0,
sum: 0f64,
}
}
fn next(&mut self, val: i32) -> f64 {
self.sum += val as f64;
self.sum -= self.v[self.index];
self.v[self.index] = val as f64;
self.index += 1;
if self.len < self.v.len() as f64 {
self.len += 1f64;
}
if self.index >= self.v.len() {
self.index = 0;
}
self.sum / self.len as f64
}
}