经典每日温度升高问题

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> sta = new LinkedList<>();
        int[] res = new int[temperatures.length];
        for(int i = 0 ; i < temperatures.length;i++){
            while(!sta.isEmpty() && temperatures[sta.peekLast()] < temperatures[i]){
                int index = sta.pollLast();
                res[index] = i-index; 
            }
            sta.addLast(i);
        }
        return res;
    }
}

这题就是用栈做每日弹出,就是传值的逻辑有点乱,栈存储的是数组索引,通过栈找到当日温度比较大于当日后,存储到目标数组中。

posted @ 2021-08-27 15:14  K峰  Views(45)  Comments(0)    收藏  举报