Skip to content

739. 每日温度 ​

  • 题号:739
  • 来源:LeetCode
  • 难度:中等
  • 标签:单调栈 数组
  • 语言:TypeScript
  • 解法:1 个
  • 作者:lmliheng
  • 最近更新:2026-09-27

TypeScript · O(n) 时间 · O(n) 空间 · 更新于 2026-09-27

单调栈存下标,出栈时得到等待天数

typescript
/**
 * @difficulty medium
 * @tags 单调栈,数组
 * @time O(n)
 * @space O(n)
 * @note 单调栈存下标,出栈时得到等待天数
 * @739. 每日温度
 */
let temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
let stack: number[] = []
let res = new Array(temperatures.length).fill(0)
for (let i = 0; i < temperatures.length; i++) {
    while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
        let top = stack.pop()!
        res[top] = i - top
    }
    stack.push(i)
}
console.log(res)

源码:ts/leetcode/739. 每日温度.ts


在 GitHub 上查看题目所在目录:lmliheng/algorithm