739. 每日温度 + 栈的使用 + 思维

739. 每日温度

LeetCode_739

相似题目:单调栈结构(进阶)

题目描述

代码实现

class Solution {
    public int[] dailyTemperatures(int[] T) {
        int n = T.length;
        Stack<Integer> sta = new Stack<>();
        int[] result = new int[n];
        for(int i=n-1; i>=0; i--){
            while(!sta.isEmpty() && T[sta.peek()] <= T[i]){
                sta.pop();
            }
            if(!sta.isEmpty()){
                result[i] = sta.peek() - i;
            }else{
                result[i] = 0;
            }
            sta.push(i);
        }
        return result;
    }
}
posted @ 2021-03-26 21:42  Garrett_Wale  阅读(32)  评论(0编辑  收藏  举报