algorithm-base/animation-simulation/单调队列单调栈/leetcode739每日温度.md

69 lines
2.8 KiB
Java
Raw Normal View History

2021-03-20 11:38:55 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [739. ](https://leetcode-cn.com/problems/daily-temperatures/)
2021-03-19 08:36:59 +00:00
> 0
1
> temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
>
> arr = [1, 1, 4, 2, 1, 1, 0, 0]
2
> temperatures = [30,30,31,45,31,34,56]
>
> arr = [2,1,1,3,1,1,0]
####
temperatures[0] = 30, 30 3131 230 0 arr[0] = 2
69726972 arr69 4725 arr[4] = 5 - 4 = 1
![](https://img-blog.csdnimg.cn/20210319163137996.gif)
便
```java
class Solution {
public int[] dailyTemperatures(int[] T) {
int len = T.length;
if (len == 0) {
return T;
}
Stack<Integer> stack = new Stack<>();
int[] arr = new int[len];
int t = 0;
for (int i = 0; i < len; i++) {
//单调栈
while (!stack.isEmpty() && T[i] > T[stack.peek()]){
arr[stack.peek()] = i - stack.pop();
}
stack.push(i);
}
return arr;
}
}
```