代码随想录第48天

739. 每日温度

python 复制代码
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures)
        ans = [0] * n
        st = []
        for i in range(n - 1, -1, -1):
            t = temperatures[i]
            while st and t >= temperatures[st[-1]]:
                st.pop()
            if st:
                ans[i] = st[-1] - i
            st.append(i)
        return ans

496.下一个更大元素 I

python 复制代码
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        idx = {x: i for i, x in enumerate(nums1)}
        ans = [-1] * len(nums1)
        st = []
        for x in reversed(nums2):
            while st and x >= st[-1]:
                # 由于 x 的出现,栈顶元素永远不会是左边元素的「下一个更大元素」
                st.pop()
            if st and x in idx:  # x 在 nums1 中
                ans[idx[x]] = st[-1]  # 记录答案
            st.append(x)
        return ans

503.下一个更大元素II

python 复制代码
class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        n = len(nums)
        ans = [-1] * n
        st = []
        for i in range(n * 2 - 1, -1, -1):
            x = nums[i % n]
            while st and x >= st[-1]:
                # 由于 x 的出现,栈顶元素永远不会是左边元素的「下一个更大元素」
                st.pop()
            if st and i < n:
                ans[i] = st[-1]
            st.append(x)
        return ans
相关推荐
程序员大雄学编程31 分钟前
用Python来学微积分23-微分中值定理
人工智能·python·数学·微积分
SunnyDays10111 小时前
如何使用Python编辑PDF文档:修改文本、添加图片、更新元数据等
python·编辑pdf·修改pdf文字
、、、、南山小雨、、、、1 小时前
加载YOLO模型,处理mp4视频
python·yolo·音视频
wshlp1234562 小时前
deepseek api 灵活使用
python
AI视觉网奇2 小时前
coco json 分类标注工具源代码
开发语言·python
要加油GW3 小时前
python使用vscode 需要配置全局的环境变量。
开发语言·vscode·python
B站计算机毕业设计之家3 小时前
python图像识别系统 AI多功能图像识别检测系统(11种识别功能)银行卡、植物、动物、通用票据、营业执照、身份证、车牌号、驾驶证、行驶证、车型、Logo✅
大数据·开发语言·人工智能·python·图像识别·1024程序员节·识别
快乐的钢镚子4 小时前
思腾合力云服务器远程连接
运维·服务器·python
苏打水com4 小时前
爬虫进阶实战:突破动态反爬,高效采集CSDN博客详情页数据
爬虫·python
夫唯不争,故无尤也4 小时前
三大AI部署框架对比:本地权重与多模型协作实战
人工智能·python·深度学习