Loading...

单调栈-Leetcode739每日温度

在这里插入图片描述

代码求解

求更高温度出现在几天后,这是一个严格大于的问题,需要使用小压大的单调栈。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public static int MAXN = 100001;

	public static int[] stack = new int[MAXN];

	public static int r;

	public static int[] dailyTemperatures(int[] nums){
		int n = nums.length;
		int[] ans = new int[n];
		r = 0;
		for(int i=0,cur;i<n;i++){
			while (r>0&&nums[stack[r-1]]<nums[i]) {
				cur = stack[--r];
				ans[cur]=i-cur;
			}
			stack[r++]=i;
		}
		return ans;
	}
}
Code Road Record