249 字
1 分钟
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]示例 2:
输入: temperatures = [30,40,50,60]输出: [1,1,1,0]示例 3:
输入: temperatures = [30,60,90]输出: [1,1,0]
思路
尝试写一下逻辑为什么用单调栈
-
这题暴力法两个
for要重复扫描,所以需要数据结构存储数据 -
求最近的较大值,所以需要栈
-
大的会挡住小的,小的没用了 ,所以 栈必须是单调的。
所以,这题可以得出需要单调栈
func dailyTemperatures(temperatures []int) []int { n := len(temperatures) res := make([]int, n) stack := make([]int, 0)
for i, v := range temperatures { for len(stack) > 0 && v > temperatures[stack[len(stack)-1]] { index := stack[len(stack)-1] stack = stack[:len(stack)-1] res[index] = i - index } stack = append(stack, i) } return res} LeetCode-739. 每日温度
https://sheep44044.github.io/posts/算法/栈/leetcode-739-每日温度/