欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

牛客剑指offer,栈的压入弹出

程序员文章站 2022-07-14 20:22:25
...
class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        bool bPossible =false;
        int nLength = pushV.size();
        if(!pushV.empty() && !popV.empty() && nLength > 0)
        {
            int i = 0;
            int j = 0;
            stack<int> s1;
            while(j< nLength-1)
            {
                while(s1.empty()||s1.top()!=popV[j])
                {
                    if(i== nLength-1)
                    {
                        break;
                    }
                    s1.push(pushV[i]);
                    i++;
                }
                if(s1.top()!= popV[j])
                    break;
                s1.pop();
                j++;
            }
            if(s1.empty() && j == nLength-1)
                bPossible = true;
        }
        return bPossible;
    }
};
相关标签: 剑指offer刷题