OJ链接: 739. 每日温度
示例代码:
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int length = temperatures.length;
//数组存储下标记录
int[] ans = new int[length];
Stack<Integer> stack = new Stack<>();
for(int i =0 ; i<length ;i++){
//遍历每个温度
int temp = temperatures[i];
//当栈不为空 且 当前温度大于栈顶温度
while( !stack.empty() && temp > temperatures[stack.peek()]){
//出栈 ,并记录下标
int perv = stack.pop();
ans[perv] = i - perv;
}
stack.push(i);
}
return ans;
}
}