Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 810 Bytes

2022-12-18.md

File metadata and controls

25 lines (22 loc) · 810 Bytes

题目

  1. Daily Temperatures

Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

思路

代码

var dailyTemperatures = function(temperatures) {
  const stack = []
  for (let i = 0; i < temperatures.length; i++) {
    while (stack.length > 0 && temperatures[i] > stack[stack.length - 1][0]) {
      const [temp, index] = stack.pop()
      temperatures[index] = i - index
    }
    stack.push([temperatures[i], i])
  }
  while (stack.length > 0) {
    const [temp, index] = stack.pop()
    temperatures[index] = 0
  }
  return temperatures
}