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; }}