739.每日温度(栈M)

/**
 * @param {number[]} T
 * @return {number[]}
 */
 var dailyTemperatures = function (T) {
    const len = T.length
    let res = new Array(len).fill(0)
    let stack = []
    for (let i = 0; i < len; i++) {
        //如果栈不为空 且当前元素大于T下标为栈顶的元素  
        while(stack.length && T[i] > T[stack[stack.length - 1]]){
            let j = stack.pop()
            res[j] = i - j  
        }
        stack.push(i)
    }
    return res
}

 

posted @ 2021-04-09 13:56  心中有一海  阅读(32)  评论(0编辑  收藏  举报