leetcode 739 每日温度 单调栈

leetcode 739 每日温度 单调栈

 

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n=temperatures.size();
        vector<int> res(n,0);
        stack<int> s;
        //从后往前 单调栈写法
        for(int i=n-1;i>=0;i--)
        {
            while(!s.empty() && temperatures[i]>=temperatures[s.top()])
            {
                s.pop();
            }
            if(!s.empty())
            {
                res[i]=s.top()-i;
            }
            s.push(i);
        }
        //从前往后单调栈写法
        /*
        for(int i=0;i<n;i++)
        {
            while(!s.empty() && temperatures[i]>temperatures[s.top()])
            {
                res[s.top()]=i-s.top();
                s.pop();
            }
            s.push(i);
        }
        */
        return res;
    }
};

 

 

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
leetcode 739 每日温度 单调栈 leetcode 739 每日温度 单调栈 leetcode 739 每日温度 单调栈 leetcode 739 每日温度 单调栈   TRANSLATE with leetcode 739 每日温度 单调栈 COPY THE URL BELOW leetcode 739 每日温度 单调栈 leetcode 739 每日温度 单调栈 Back EMBED THE SNIPPET BELOW IN YOUR SITE leetcode 739 每日温度 单调栈 Enable collaborative features and customize widget: Bing Webmaster Portal Back
上一篇:LeetCode 739 每日温度题解


下一篇:Javascript 中的false,零值,null,undefined和空字符串对象