当前位置:网站首页>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 ……
边栏推荐
- BUU-Crypto-[HDCTF2019]basic rsa
- Simulink and Arduino serial port communication
- js如何将秒转换成时分秒显示
- SQL injection - injection based on MSSQL (SQL Server)
- Compound nonlinear feedback control (2)
- Nexus 6p从8.0降级6.0+root
- Penetration tool - sqlmap
- [QT] create mycombobox click event
- How to configure static IP for Kali virtual machine
- JS扁平化数形结构的数组
猜你喜欢

JS扁平化数形结构的数组

1480. 一维数组的动态和

C # character similarity comparison general class

Online shrimp music will be closed in January next year. Netizens call No

gslb(global server load balance)技术的一点理解

How to get the parent node of all nodes in El tree

Accidentally deleted the data file of Clickhouse, can it be restored?

【微服务】Nacos集群搭建以及加载文件配置

VB.net 简单的处理图片,黑白(类库——7)
![BUU-Crypto-[GUET-CTF2019]BabyRSA](/img/87/157066155e8d3a93e30a68eaf1781b.jpg)
BUU-Crypto-[GUET-CTF2019]BabyRSA
随机推荐
ES6 模块化
LC weekly 300
left_and_right_net正常版本
Halcon image calibration enables subsequent image processing to become the same as the template image
XII Golang others
ANSYS command
十二. golang其他
HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
光模塊字母含義及參數簡稱大全
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
Arc135 C (the proof is not very clear)
Take you to quickly learn how to use qsort and simulate qsort
Component、Container容器常用API详解:Frame、Panel、ScrollPane
Supplement the JS of a video website to decrypt the video
FRP intranet penetration, reverse proxy
复合非线性反馈控制(二)
19.Frambuffer应用编程
JS how to convert seconds into hours, minutes and seconds display
js获取对象中嵌套的属性值
fastjson