zwkcoding / 100Days-Of-Leetcode

就像电影(500)Days of Summer一样,记录着每一天 Leetcode @itgoyo/500Days-Of-Github
0 stars 0 forks source link

[Undone]Explore00_Stack_Daily Temperatures_Chapter03 #9

Open zwkcoding opened 5 years ago

zwkcoding commented 5 years ago

Question: Problem see here : https://leetcode.com/explore/learn/card/queue-stack/230/usage-stack/1363/

leave the hole: use the Data Structure Stack,

initial ideas:

zwkcoding commented 5 years ago

Solution:

/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& T) {
        stack<int> sta;
        vector<int> ans;
        ans.resize(T.size(), 0);
        for(int i=0;i<T.size();i++){
            while(sta.empty() != true) {
                if(T[sta.top()] < T[i])  {
                    ans[sta.top()] = i - sta.top();
                    sta.pop();
                } else {
                    break;
                }
            }
            sta.push(i);
        }
        return ans;
    }

};