当前位置:网站首页>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 ……
边栏推荐
- [QT] create mycombobox click event
- VB.net 调用FFmpeg简单处理视频(类库——6)
- Arc135 a (time complexity analysis)
- Experience weekly report no. 102 (July 4, 2022)
- MySQL的information_schema数据库
- Qt发布多语言国际化翻译
- Invalid revision: 3.18.1-g262b901-dirty
- 19.Frambuffer应用编程
- How to implement lazy loading in El select (with search function)
- js获取对象中嵌套的属性值
猜你喜欢
随机推荐
Letter meaning and parameter abbreviation of optical module Daquan
Take you to quickly learn how to use qsort and simulate qsort
Viewing and using binary log of MySQL
BUU-Crypto-[GXYCTF2019]CheckIn
How to get the parent node of all nodes in El tree
JS flattened array of number shape structure
Easy change
JS arguments parameter usage and explanation
gslb(global server load balance)技术的一点理解
Basic concept of bus
Wechat applet +php realizes authorized login
How to configure static IP for Kali virtual machine
Arc135 C (the proof is not very clear)
The end of the Internet is rural revitalization
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
Thinkphp6.0 middleware with limited access frequency think throttle
How to expand all collapse panels
VB.net 简单的处理图片,黑白(类库——7)
19. Framebuffer application programming
Compound nonlinear feedback control (2)