0
点赞
收藏
分享

微信扫一扫

leetcode 739每日温度

谁知我新 2022-04-13 阅读 207
leetcode

用一个顺序栈来进行辅助工作。

当t[i]>stk.top()时,弹出。

每个i都要进栈。

class Solution {
public:
    stack<int> stk;
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stk.push(0);
        vector<int> res(temperatures.size());
        for (int i=1;i<temperatures.size();i++){
            if (!stk.empty()){
                while (!stk.empty() && temperatures[i]>temperatures[stk.top()]){
                    int t=stk.top();
                    stk.pop();
                    res[t]=i-t;
                }
            }
            stk.push(i);
        }
        return res;

    }
};
举报

相关推荐

0 条评论