zoukankan      html  css  js  c++  java
  • 队列&栈//每日温度

    根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数。如果之后都不会升高,请输入 0 来代替。

    例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]

    提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的都是 [30, 100] 范围内的整数。

    class Solution {
        public int[] dailyTemperatures(int[] temperatures) {
            Stack<Entry> stack = new Stack<>();
            int[] res = new int[temperatures.length];
            for(int i = 0; i < temperatures.length; i++){
                if(stack.isEmpty()){
                    stack.push(new Entry(temperatures[i],i));
                    continue;
                }
                if(temperatures[i] <= stack.peek().val)
                    stack.push(new Entry(temperatures[i],i));
                else
                {
                    int j = 1;
                    while(!stack.isEmpty()&&temperatures[i] > stack.peek().val){
                        Entry tmp = stack.pop();
                        res[tmp.index] = i - tmp.index;
                    }
                    stack.push(new Entry(temperatures[i],i));
                }
            }
            return res;
        }
        private class Entry{
            public int val;
            public int index;
            public Entry(int val,int index){
                this.val = val;
                this.index = index;
            }
        }
    }
    class Solution {
    public:
        vector<int> dailyTemperatures(vector<int>& temperatures) {
            vector<int> result;
            stack<int> record;
            for(int i = temperatures.size()-1; i>=0; i--){
                while(record.size()>0&&temperatures[record.top()]<=temperatures[i])
                {
                    record.pop();
                }
                int wait_day = record.size()==0?0:record.top()-i;
                result.insert(result.begin(),wait_day);
                record.push(i);
            }
            return result;
        }
    };
  • 相关阅读:
    python中获取今天昨天和明天的日期
    Redis安装系统服务1073错误
    npm 安装删除模块
    git 远程服务器创建项目自动化部署、克隆推送免密码
    A20地址线科普【转载】
    fork 与 vfork
    /bin /usr/bin /sbin /usr/sbin 目录的作用
    Coreutils
    VMware 设置支持UEFI
    npm 发布包
  • 原文地址:https://www.cnblogs.com/strawqqhat/p/10602362.html
Copyright © 2011-2022 走看看