LeetCode 739:每日温度

解题思路有两点比较关键:

1、想到用栈,而且栈中存放的是索引,而不是温度值

2、打破思维桎梏,不是按顺序来计算D中每一个值的 

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        vector<int> D(T.size(), 0);
        stack<int> temp;

        temp.push(0);
        int index;
        for(int i=1; i<T.size(); i++) {
            while(!temp.empty() && T[temp.top()] < T[i]) {
                D[temp.top()] = i - temp.top();
                temp.pop();
            }
            temp.push(i);
        }

        return D;
    }
};
发布了97 篇原创文章 · 获赞 11 · 访问量 2473

猜你喜欢

转载自blog.csdn.net/chengda321/article/details/104150525