当前位置:网站首页>Sword finger offer II 038 Daily temperature
Sword finger offer II 038 Daily temperature
2022-07-04 05:52:00 【Small white yards fly up】
Summary
Use stack , Traversal array , Compare whether the current number is larger than the top element . Big rule out stack processing , Until the stack is empty or the current number is less than the number at the top of the stack , Put the current number on the stack .
subject
Please according to the daily The temperature list temperatures , Rebuild a list , The output of its corresponding position is required to be : To see a higher temperature , At least the number of days to wait . If the temperature doesn't rise after that , Please use... In this position 0 Instead of .
link :https://leetcode.cn/problems/iIQa4I
Ideas
In fact, it is very similar to the collision of planets .
We use a stack to store the subscript of each element in the array , Start by traversing the array .
- If the stack is empty , The stack .
- If the stack is not empty , Compare the top of the stack with the size of the current element
- If the current element is large , Then subtract the current element subscript from the stack top subscript , Get the stack top element “ Number of days to wait ”. Then continue to compare the size with the elements in the stack .
- If the current element is small , The stack .
In this way, the element at the top of the stack is always the smallest , Once a larger value appears , Just dispose of all values smaller than him in the stack .
solution : Monotonic stack
Code
public int[] dailyTemperatures2(int[] temperatures) {
int[] result = new int[temperatures.length];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < temperatures.length; i++) {
if (stack.isEmpty()) {
stack.push(i);
continue;
}
int current = temperatures[i];
while (!stack.isEmpty() && current > temperatures[stack.peek()]) {
int index = stack.pop();
result[index] = i - index;
}
stack.push(i);
}
// Deal with days when high temperatures cannot be observed
while (!stack.isEmpty()) {
result[stack.pop()] = 0;
}
return result;
}
Execution results
Note here , Directly used Deque Instead of Stack, because Stack The efficiency of is too low ……
边栏推荐
- Supplement the JS of a video website to decrypt the video
- Online shrimp music will be closed in January next year. Netizens call No
- Viewing and using binary log of MySQL
- Leetcode question brushing record | 206_ Reverse linked list
- LC周赛300
- win10清除快速访问-不留下痕迹
- How to configure static IP for Kali virtual machine
- Configure cross compilation tool chain and environment variables
- 光模塊字母含義及參數簡稱大全
- webrtc 快速搭建 视频通话 视频会议
猜你喜欢
检漏继电器JY82-2P
[microservice] Nacos cluster building and loading file configuration
Letter meaning and parameter abbreviation of optical module Daquan
BUU-Reverse-easyre
Simulink and Arduino serial port communication
transformer坑了多少算力
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
Take you to quickly learn how to use qsort and simulate qsort
What are the reasons for the frequent high CPU of ECS?
ANSYS command
随机推荐
transformer坑了多少算力
BUU-Crypto-[GXYCTF2019]CheckIn
Introduction to AMBA
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
卸载Google Drive 硬盘-必须退出程序才能卸载
JS how to convert seconds into hours, minutes and seconds display
FRP intranet penetration, reverse proxy
Build an Internet of things infrared temperature measuring punch in machine with esp32 / rush to work after the Spring Festival? Baa, no matter how hard you work, you must take your temperature first
Accidentally deleted the data file of Clickhouse, can it be restored?
Arc135 C (the proof is not very clear)
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
1480. 一维数组的动态和
注释与注解
js arguments参数使用和详解
left_and_right_net可解释性设计
一键过滤选择百度网盘文件
谷歌 Chrome 浏览器将支持选取文字翻译功能
One click filtering to select Baidu online disk files
Weekly summary (*63): about positive energy
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend