当前位置:网站首页>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 ……
边栏推荐
- fastjson
- 19.Frambuffer应用编程
- VB.net 简单的处理图片,黑白(类库——7)
- Introduction to AMBA
- VB. Net simple processing pictures, black and white (class library - 7)
- HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
- webrtc 快速搭建 视频通话 视频会议
- 复合非线性反馈控制(二)
- How to solve the component conflicts caused by scrollbars in GridView
- SQL performance optimization skills
猜你喜欢
![[excel] PivotChart](/img/45/be87e4428a1d8ef66ef34a63d12fd4.png)
[excel] PivotChart

Design and implementation of redis 7.0 multi part AOF

接地继电器DD-1/60

724. Find the central subscript of the array

How to expand all collapse panels

BUU-Pwn-test_ your_ nc

BUU-Reverse-easyre

Uninstall Google drive hard drive - you must exit the program to uninstall

The end of the Internet is rural revitalization
![[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)](/img/78/63ab1a8bb1b6e256cc740f3febe711.jpg)
[wechat applet] template and configuration (wxml, wxss, global and page configuration, network data request)
随机推荐
[MySQL practice of massive data with high concurrency, high performance and high availability -8] - transaction isolation mechanism of InnoDB
How to determine whether an array contains an element
js arguments参数使用和详解
Arc135 a (time complexity analysis)
webrtc 快速搭建 视频通话 视频会议
每周小结(*63):关于正能量
HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
Redis realizes ranking function
Upper computer software development - log information is stored in the database based on log4net
力扣(LeetCode)184. 部门工资最高的员工(2022.07.03)
注释与注解
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
BUU-Crypto-[GXYCTF2019]CheckIn
Input displays the currently selected picture
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
How much computing power does transformer have
[microservice] Nacos cluster building and loading file configuration
AWT常用组件、FileDialog文件选择框
十二. golang其他
How to clone objects