当前位置:网站首页>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 .

边栏推荐
- Problems of font legend and time scale display of MATLAB drawing coordinate axis
- Kindle operation: transfer downloaded books and change book cover
- Kafka-connect将Kafka数据同步到Mysql
- Guojingxin center "APEC education +" Shanghai Jiaotong University Japan Cooperation Center x Fudan philosophy class "Zhe Yi" 2022 New Year greetings
- Qml 字体使用pixelSize来自适应界面
- ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
- 2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
- Euler Lagrange equation
- Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
- Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
猜你喜欢
![[loss function] entropy / relative entropy / cross entropy](/img/bc/574a4745336b0baf1a4ca53af41a82.jpg)
[loss function] entropy / relative entropy / cross entropy

如何让导电滑环信号更好

Problems of font legend and time scale display of MATLAB drawing coordinate axis

COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立

2、TD+Learning

为什么更新了 DNS 记录不生效?

The solution of frame dropping problem in gnuradio OFDM operation

Redis cluster

从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值

2、TD+Learning
随机推荐
Common operations of numpy on two-dimensional array
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
5. Discrete control and continuous control
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Matlab method is good~
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Android 创建的sqlite3数据存放位置
Redux使用
Working principle of stm32gpio port
2022 R1 fast opening pressure vessel operation test question bank and R1 fast opening pressure vessel operation free test questions
2021 welder (primary) examination skills and welder (primary) operation examination question bank
NPM internal split module
2022 safety officer-c certificate examination summary and safety officer-c certificate reexamination examination
Application of state mode in JSF source code
The usage of rand function in MATLAB
由排行榜实时更新想到的数状数值
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Gnuradio3.9.4 create OOT module instances
break net
Gnuradio 3.9 using OOT custom module problem record