LeetCode-739-每日温度
思路
理解一下题意,题目要的是求出当前元素右边第一个大于该元素的位置差,对于这种情况,一般可以使用单调栈来解决。
维护一个单调栈
r[s.top]<r[i],出栈,计算距离
r[s.top]>=r[i],进栈
代码
public int[] dailyTemperatures(int[] T) {
Stack<Integer> st=new Stack<>();
int []r=new int[T.length];
for(int i=0;i<T.length;i++){
while(!st.isEmpty()&&T[st.peek()]<T[i]){
r[st.peek()]=i-st.pop();
}
st.push(i);
}
return r;
}