当前位置:网站首页>Sword finger offer II 041. Average value of sliding window_____ Using queue / loop array implementation
Sword finger offer II 041. Average value of sliding window_____ Using queue / loop array implementation
2022-07-25 03:20:00 【To the light】
The finger of the sword Offer II 041. Average value of sliding window
Given an integer data stream and a window size , Depending on the size of the sliding window , Calculate the average of all numbers in the sliding window .
Realization MovingAverage class :
MovingAverage(int size) Use window size size Initialize object .
double next(int val) Member functions next An integer will be added to the sliding window every time , Please calculate and return the last... In the data stream size Moving average of values , That is, the average of all numbers in the sliding window .
Example :
Input :
inputs = [“MovingAverage”, “next”, “next”, “next”, “next”]
inputs = [[3], [1], [10], [3], [5]]
Output :
[null, 1.0, 5.5, 4.66667, 6.0]
explain :
MovingAverage movingAverage = new MovingAverage(3);
movingAverage.next(1); // return 1.0 = 1 / 1
movingAverage.next(10); // return 5.5 = (1 + 10) / 2
movingAverage.next(3); // return 4.66667 = (1 + 10 + 3) / 3
movingAverage.next(5); // return 6.0 = (10 + 3 + 5) / 3
Tips :
- 1 <= size <= 1000
- -105 <= val <= 105
- Call at most next Method 104 Time
Solution1( Using the queue ):
- Use queue structure directly ;
Code1:
/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */
class MovingAverage {
Queue<Integer> queue;
int len;
/** Initialize your data structure here. */
public MovingAverage(int size) {
queue = new LinkedList<>();
len = size;
}
public double next(int val) {
if(queue.size() < len){
queue.offer(val);
}
else{
queue.poll();
queue.offer(val);
}
double res = 0;
// Because the essence of queue is LinkedList, So you can't access directly according to subscripts like arrays
for(Integer one : queue){
res += one;
}
return res / queue.size();
}
}
Solution2( Using the queue + Space for time preprocessing ):
- Because of method 1, we need to calculate the average value every time , So we might as well create a variable directly res To maintain the total size of the elements in the queue , In this way, the average value of sliding window can be obtained directly when searching , also res Just execute every time next Always follow ++ that will do , No extra time ;
Code2:
/** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */
class MovingAverage {
Queue<Integer> queue;
int len;
double res;
/** Initialize your data structure here. */
public MovingAverage(int size) {
queue = new LinkedList<>();
len = size;
res = 0;
}
public double next(int val) {
if(queue.size() < len){
queue.offer(val);
res += val;
}
else{
res -= queue.poll();
queue.offer(val);
res += val;
}
return res / queue.size();
}
}
Solution3( Use circular arrays to simulate queues + Space for time preprocessing ):
- We can use circular arrays to simulate queues , Through the remainder operation, the array achieves the recyclable effect , To simulate the queue ;
Code3:
class MovingAverage {
int[] rep;
int front;
int rear;
int len;
double res;
/** Initialize your data structure here. */
public MovingAverage(int size) {
rep = new int[size];
front = 0;
rear = 0;
res = 0;
}
public double next(int val) {
// First judge whether the queue is full
int repLen = rep.length;
if(len < repLen){
rep[rear] = val;
rear = (rear + 1) % repLen; // The remainder has the effect of circulation
len++;
}
else{
res -= rep[front];
front = (front + 1) % repLen;
rep[rear] = val;
rear = (rear + 1) % repLen;
}
res += val;
return res / len;
}
}
边栏推荐
- ECMAScript new features
- Solution: owner's smart site supervision cloud platform
- Riotboard development board series notes (VIII) -- building desktop system
- mysql_ Master slave synchronization_ Show slave status details
- mysql_ Record the executed SQL
- Use go language to import Doris data through stream load
- JS construction linked list
- Innobackupex parameter description
- JS written test -- regular expression
- Lombok detailed introduction
猜你喜欢

Learning record XIII

JS construct binary tree

Preliminary foundation JVM

Detailed explanation of three factory modes

Color space (1) - RGB

Banana pie bpi-m5 toss record (2) -- compile u-boot

How is the virtual DOM different from the actual DOM?

File permission management

Consistent hash, virtual node, bloom filter
![[template engine] microservice Learning Notes 6: freemaker](/img/6a/cfe9c5aea0f7fc83d0812237de2256.png)
[template engine] microservice Learning Notes 6: freemaker
随机推荐
Web -- JDBC tool class writing
JS password combination rule - 8-16 digit combination of numbers and characters, not pure numbers and pure English
Riotboard development board series notes (6) -- buildreoot building system image
Banana pie bpi-m5 toss record (2) -- compile u-boot
Preliminary foundation JVM
Record once C # extract audio files with ffmempeg
Flink1.15 source code reading - Flink annotations
Use and introduction of vim file editor
Task02 | EDA initial experience
Learning Record V
Use of stm32cubemonitor part I - data plotting and instrument display
JS construction linked list
Reasons for not sending requests after uni app packaging
Use of stm32cubemonitor Part II - historical data storage and network access
Idea configuration
Brief understanding of operational amplifier
Page performance: how to optimize pages systematically?
Innobackupex parameter description
Matplotlib tutorial (I) [getting to know Matplotlib first]
Why does the legend of superstar (Jay Chou) not constitute pyramid selling? What is the difference between distribution and pyramid selling?