您的位置:首页 > 新闻 > 会展 > 投票小程序_传奇手游网页版_外链网盘_小程序开发

投票小程序_传奇手游网页版_外链网盘_小程序开发

2025/7/26 20:43:39 来源:https://blog.csdn.net/qq_65509025/article/details/146966889  浏览:    关键词:投票小程序_传奇手游网页版_外链网盘_小程序开发
投票小程序_传奇手游网页版_外链网盘_小程序开发

1、题目描述

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。

示例 1:

输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]

示例 2:

输入: temperatures = [30,40,50,60]
输出: [1,1,1,0]

2、初始思路

2.1 思路

采用单调栈的方法及逆行优化,单调栈也就是(单调性+栈)。

2.1.1 从右到左 

2.1.2 从左到右

3 完整代码

3.1.1 从右到左

class Solution:def dailyTemperatures(self, temperatures: List[int]) -> List[int]:n = len(temperatures)ans = [0] * nstack = []for i in range(n-1, -1, -1):while stack and temperatures[i] >= temperatures[stack[-1]]:stack.pop()if stack:ans[i] = stack[-1] - istack.append(i)return ans

3.1.2 从左到右

class Solution:def dailyTemperatures(self, temperatures: List[int]) -> List[int]:n = len(temperatures)ans = [0] * nstack = []for i in range(n):while stack and temperatures[i] > temperatures[stack[-1]]:j = stack.pop()ans[j] = i - jstack.append(i)return ans

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com