题解 | #每日温度#

每日温度

https://www.nowcoder.com/practice/1f54e163e6944cc7b8759cc09e9c78d8

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 每日温度
     * @param dailyTemperatures int整型一维数组
     * @return int整型一维数组
     */
    public int[] temperatures (int[] dailyTemperatures) {
        // write code here
        // 解题思路:采用栈,后进先出
        if (dailyTemperatures == null || dailyTemperatures.length == 0) {
            return null;
        }

        int length = dailyTemperatures.length;
        int[] result = new int[length];
        Stack<Integer> stack = new Stack<>();
        stack.push(0);

        for (int i = 1; i < length; i++) {

            while (!stack.isEmpty() &&
                    dailyTemperatures[stack.peek()] < dailyTemperatures[i]) {
                int index = stack.pop();
                result[index] = i - index;
            }

            stack.push(i);
        }


        return result;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-18 17:37
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务