关于leetcode个人解题总结:刷题5每日温度

Leetcode: 739. 每日温度

要点:

把数组下标压进栈,行将进栈的下标元素对应的温度如果比栈顶元素对应的温度高,则该元素为左边第一个比栈顶元素对应温度高的温度下标,该元素与栈顶元素下标之差即为须要期待的天数,其余元素采纳雷同做法,最终后果放在数组res中。

class Solution {
    public int[] dailyTemperatures(int[] T) {
        Stack<Integer> st = new Stack<Integer>();
        int len = T.length;
        int[] res = new int[len];
        Arrays.fill(res,0);

        for(int i = 0;i < len;i++){
            while(!st.isEmpty() && T[st.peek()] < T[i]){
                int index = st.peek();
                int day = i - st.pop();
                res[index] = day;
            }
            st.push(i);
        }

        return res;
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理