首页 > 代码库 > 剑指offer---栈的压入,弹出顺序

剑指offer---栈的压入,弹出顺序

class Solution 
{
public:
    bool IsPopOrder(vector<int> pushV, vector<int> popV)
    {

        stack<int> stack1;
        
        int i = 0;
        int j = 0;

        stack1.push(pushV[0]);
        while ((!stack1.empty()) && (j<popV.size()))
        {
        
            if ((stack1.top() != popV[j]) && (i<pushV.size()))
            {
                ++i;
                stack1.push(pushV[i]);
            }
            else if (stack1.top() == popV[j])
            {
                stack1.pop();
                ++j;
            }
            else
            {
                ++j;
            }
        }
        
        if (stack1.empty()) return true;
        else return false;
    }
};

 

剑指offer---栈的压入,弹出顺序