当前位置:网站首页>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 ……
边栏推荐
- Grounding relay dd-1/60
- JS arguments parameter usage and explanation
- Google Chrome browser will support the function of selecting text translation
- JS扁平化数形结构的数组
- How to solve the component conflicts caused by scrollbars in GridView
- BUU-Crypto-Cipher
- Weekly summary (*63): about positive energy
- px em rem的区别
- left_and_right_net正常版本
- js arguments参数使用和详解
猜你喜欢

BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
![[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)

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

VB. Net simple processing pictures, black and white (class library - 7)

19.Frambuffer应用编程

What are the reasons for the frequent high CPU of ECS?

VB.net 简单的处理图片,黑白(类库——7)

Actual cases and optimization solutions of cloud native architecture

体验碎周报第 102 期(2022.7.4)

注释与注解
随机推荐
VB. Net calls ffmpeg to simply process video (class Library-6)
fastjson
JS arguments parameter usage and explanation
SQL performance optimization skills
VB.net 调用FFmpeg简单处理视频(类库——6)
如何展开Collapse 的所有折叠面板
VB. Net simple processing pictures, black and white (class library - 7)
left_and_right_net正常版本
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
How to implement lazy loading in El select (with search function)
AWT常用组件、FileDialog文件选择框
如何获取el-tree中所有节点的父节点
19.Frambuffer应用编程
js如何将秒转换成时分秒显示
C language simple student management system (including source code)
ansys命令
19. Framebuffer application programming
Redis realizes ranking function
Take you to quickly learn how to use qsort and simulate qsort
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs