LeetCode 739. 每日温度

本文解析了LeetCode题目739每日温度,通过遍历和栈的操作,找出每个温度之后更高的日子。介绍了思路、时间复杂度及代码实现,并涵盖了关键知识点:数组操作、温度比较算法。

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.知识点



总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大涛小先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值