链接
题目描述
递减栈(O(N))
视频
一视频胜千言
遍历一次数组,构造递减栈,如果当前数字大于栈顶,出栈,计算栈顶坐标对应的结果;如果小于栈顶,入栈
class Solution {
public int[] dailyTemperatures(int[] T) {
int[] res = new int[T.length];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < T.length ;i++){
while(!stack.isEmpty() && T[i] > T[stack.peek()]){
int pre = stack.pop();
res[pre] = i-pre;
}
stack.push(i);
}
return res;
}
}