当前位置:网站首页>Sword finger offer II 041 Average value of sliding window
Sword finger offer II 041 Average value of sliding window
2022-07-08 01:41:00 【Small white yards fly up】
A word summary
Using queue implementation , Ensure that the queue length is the size of the sliding window . That is, the element is removed from the front of the queue after it exceeds .
subject
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 .
link :https://leetcode.cn/problems/qIsx9U
Ideas
That is to say , The window size is fixed , When the number of elements in the window is less than the window size , Add integers to the window , And calculate the average value directly .
If you add an integer , The number exceeds the window size , You need to eliminate the first element .
fifo , That's the queue .
solution : Using the queue
Code
public class MovingAverage {
Queue<Integer> queue = new LinkedList<>();
int maxSize;
int size;
int total;
/** * Initialize your data structure here. */
public MovingAverage(int size) {
maxSize = size;
}
public double next(int val) {
// Current value is queued
queue.add(val);
total += val;
if (size >= maxSize) {
Integer poll = queue.poll();
total = total - poll;
} else {
size++;
}
return (double) total / (double) size;
}
}
Submit results
Expand your knowledge : queue
Commonly used api
queue , First in, first out .
Java The queue in , Need to achieve Queue
This interface .
Commonly used api The definition of :
boolean offer(E e)
Add elements at the end of the team . If there is not enough space, return false.E poll()
The team leader removes the element . If the queue is empty , Then return to null.E peek()
Get team leader elements , Do not remove . If the queue is empty , Then return to null.boolean add(E e)
Add elements at the end of the team . If there is not enough space, an exception will be thrown .E remove()
The team leader removes the element . If the queue is empty , Throw an exceptionE element()
Get team leader elements , Do not remove . If the queue is empty , Throw an exception
We often use the implementation , It's all used LinkedList
To achieve Queue
. And because LinkedList
It's a chain , Therefore, it does not involve the limitation of space ,offer The implementation of a method is essentially a call add Method .
边栏推荐
- 2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
- DataWorks值班表
- Understanding of maximum likelihood estimation
- QT -- package the program -- don't install qt- you can run it directly
- 2、TD+Learning
- Mat file usage
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- 用户之声 | 对于GBase 8a数据库学习的感悟
- Probability distribution
- 2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
猜你喜欢
The persistence mode of redis - RDB and AOF persistence mechanisms
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
2022 examination for safety production management personnel of hazardous chemical production units and new version of examination questions for safety production management personnel of hazardous chem
用户之声 | 对于GBase 8a数据库学习的感悟
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
Understanding of expectation, variance, covariance and correlation coefficient
break net
Chapter 7 behavior level modeling
随机推荐
Gbase observation | how to protect the security of information system with frequent data leakage
About snake equation (3)
php 获取音频时长等信息
Redis集群
Matlab code on error analysis (MAE, MAPE, RMSE)
Different methods for setting headers of different pages in word (the same for footer and page number)
2021-03-06 - play with the application of reflection in the framework
A little experience from reading "civilization, modernization, value investment and China"
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Plot function drawing of MATLAB
Call (import) in Jupiter notebook ipynb . Py file
Matlab method is good~
5. Contrôle discret et contrôle continu
Qt - - Packaging Programs - - Don't install Qt - can run directly
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
第七章 行为级建模
Is it safe to open an account on your mobile phone for small amount of stock speculation?
qt--将程序打包--不要安装qt-可以直接运行