LeetCode 739. 每日温度
题目描述
给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指在第 i 天之后,才会有更高的温度。如果气温在这之后都不会升高,请在该位置用 0 来代替。
示例 1:
输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]
每日温度
提示:
1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100
一、解题关键词
数组 、
二、解题报告
1.思路分析
核心在于值的比较 比较之后,坐标差值存到结果当中
2.时间复杂度
3.代码示例
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int len =temperatures.length;
int[] ans = new int[len];
Deque<Integer> stack = new LinkedList<Integer>();
for(int i = 0; i < len ; i++){
int temperature = temperatures[i];
while(!stack.isEmpty() && temperature > temperatures[stack.peek()]){
int preIndex = stack.pop();
ans[preIndex] = i - preIndex;
}
stack.push(i);
}
return ans;
}
}
2.知识点
本文解析了LeetCode题目739每日温度,通过遍历和栈的操作,找出每个温度之后更高的日子。介绍了思路、时间复杂度及代码实现,并涵盖了关键知识点:数组操作、温度比较算法。
1243

被折叠的 条评论
为什么被折叠?



