Skip to content

Commit d530f19

Browse files
committed
feat: add solutions to lcof2 problem: No.041
1 parent 6102880 commit d530f19

File tree

11 files changed

+484
-24
lines changed

11 files changed

+484
-24
lines changed

lcof2/剑指 Offer II 041. 滑动窗口的平均值/README.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,148 @@ movingAverage.next(5); // 返回 6.0 = (10 + 3 + 5) / 3
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
“循环数组/队列”实现。
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

5860
<!-- 这里可写当前语言的特殊实现逻辑 -->
5961

6062
```python
61-
63+
class MovingAverage:
64+
65+
def __init__(self, size: int):
66+
"""
67+
Initialize your data structure here.
68+
"""
69+
self.size = size
70+
self.data = [0] * size
71+
self.sum = 0
72+
self.count = 0
73+
74+
def next(self, val: int) -> float:
75+
idx = self.count % self.size
76+
old_val = self.data[idx]
77+
self.data[idx] = val
78+
self.sum += val - old_val
79+
self.count += 1
80+
return self.sum / min(self.count, self.size)
81+
82+
# Your MovingAverage object will be instantiated and called as such:
83+
# obj = MovingAverage(size)
84+
# param_1 = obj.next(val)
6285
```
6386

6487
### **Java**
6588

6689
<!-- 这里可写当前语言的特殊实现逻辑 -->
6790

6891
```java
92+
class MovingAverage {
93+
private int size;
94+
private int[] data;
95+
private int sum;
96+
private int count;
97+
98+
/** Initialize your data structure here. */
99+
public MovingAverage(int size) {
100+
this.size = size;
101+
this.data = new int[size];
102+
}
103+
104+
public double next(int val) {
105+
int idx = count % size;
106+
int oldVal = data[idx];
107+
data[idx] = val;
108+
sum += val - oldVal;
109+
++count;
110+
return sum * 1.0 / Math.min(count, size);
111+
}
112+
}
113+
114+
/**
115+
* Your MovingAverage object will be instantiated and called as such:
116+
* MovingAverage obj = new MovingAverage(size);
117+
* double param_1 = obj.next(val);
118+
*/
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class MovingAverage {
125+
public:
126+
/** Initialize your data structure here. */
127+
MovingAverage(int size) {
128+
this->size = size;
129+
data.resize(size);
130+
}
131+
132+
double next(int val) {
133+
int idx = count % size;
134+
int oldVal = data[idx];
135+
data[idx] = val;
136+
sum += val - oldVal;
137+
++count;
138+
return (double) sum / min(count, size);
139+
}
140+
141+
private:
142+
int size = 0;
143+
vector<int> data;
144+
int sum = 0;
145+
int count = 0;
146+
};
147+
148+
/**
149+
* Your MovingAverage object will be instantiated and called as such:
150+
* MovingAverage* obj = new MovingAverage(size);
151+
* double param_1 = obj->next(val);
152+
*/
153+
```
69154
155+
### **Go**
156+
157+
```go
158+
type MovingAverage struct {
159+
size int
160+
data []int
161+
sum int
162+
count int
163+
}
164+
165+
/** Initialize your data structure here. */
166+
func Constructor(size int) MovingAverage {
167+
return MovingAverage{
168+
size: size,
169+
data: make([]int, size),
170+
sum: 0,
171+
count: 0,
172+
}
173+
}
174+
175+
func (this *MovingAverage) Next(val int) float64 {
176+
idx := this.count % this.size
177+
oldVal := this.data[idx]
178+
this.data[idx] = val
179+
this.sum += val - oldVal
180+
this.count++
181+
return float64(this.sum) / float64(min(this.count, this.size))
182+
}
183+
184+
func min(a, b int) int {
185+
if a < b {
186+
return a
187+
}
188+
return b
189+
}
190+
191+
/**
192+
* Your MovingAverage object will be instantiated and called as such:
193+
* obj := Constructor(size);
194+
* param_1 := obj.Next(val);
195+
*/
70196
```
71197

72198
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MovingAverage {
2+
public:
3+
/** Initialize your data structure here. */
4+
MovingAverage(int size) {
5+
this->size = size;
6+
data.resize(size);
7+
}
8+
9+
double next(int val) {
10+
int idx = count % size;
11+
int oldVal = data[idx];
12+
data[idx] = val;
13+
sum += val - oldVal;
14+
++count;
15+
return (double) sum / min(count, size);
16+
}
17+
18+
private:
19+
int size = 0;
20+
vector<int> data;
21+
int sum = 0;
22+
int count = 0;
23+
};
24+
25+
/**
26+
* Your MovingAverage object will be instantiated and called as such:
27+
* MovingAverage* obj = new MovingAverage(size);
28+
* double param_1 = obj->next(val);
29+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type MovingAverage struct {
2+
size int
3+
data []int
4+
sum int
5+
count int
6+
}
7+
8+
/** Initialize your data structure here. */
9+
func Constructor(size int) MovingAverage {
10+
return MovingAverage{
11+
size: size,
12+
data: make([]int, size),
13+
sum: 0,
14+
count: 0,
15+
}
16+
}
17+
18+
func (this *MovingAverage) Next(val int) float64 {
19+
idx := this.count % this.size
20+
oldVal := this.data[idx]
21+
this.data[idx] = val
22+
this.sum += val - oldVal
23+
this.count++
24+
return float64(this.sum) / float64(min(this.count, this.size))
25+
}
26+
27+
func min(a, b int) int {
28+
if a < b {
29+
return a
30+
}
31+
return b
32+
}
33+
34+
/**
35+
* Your MovingAverage object will be instantiated and called as such:
36+
* obj := Constructor(size);
37+
* param_1 := obj.Next(val);
38+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class MovingAverage {
2+
private int size;
3+
private int[] data;
4+
private int sum;
5+
private int count;
6+
7+
/** Initialize your data structure here. */
8+
public MovingAverage(int size) {
9+
this.size = size;
10+
this.data = new int[size];
11+
}
12+
13+
public double next(int val) {
14+
int idx = count % size;
15+
int oldVal = data[idx];
16+
data[idx] = val;
17+
sum += val - oldVal;
18+
++count;
19+
return sum * 1.0 / Math.min(count, size);
20+
}
21+
}
22+
23+
/**
24+
* Your MovingAverage object will be instantiated and called as such:
25+
* MovingAverage obj = new MovingAverage(size);
26+
* double param_1 = obj.next(val);
27+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class MovingAverage:
2+
3+
def __init__(self, size: int):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.size = size
8+
self.data = [0] * size
9+
self.sum = 0
10+
self.count = 0
11+
12+
def next(self, val: int) -> float:
13+
idx = self.count % self.size
14+
old_val = self.data[idx]
15+
self.data[idx] = val
16+
self.sum += val - old_val
17+
self.count += 1
18+
return self.sum / min(self.count, self.size)
19+
20+
# Your MovingAverage object will be instantiated and called as such:
21+
# obj = MovingAverage(size)
22+
# param_1 = obj.next(val)

0 commit comments

Comments
 (0)