[C ++] - cattle-off solution to a problem of judging whether the stack of pop-up sequence

Title Description

Here Insert Picture Description
Ideas : The elements of the array simultaneously pushed on the stack, will be the top element and the inside element popV comparison, the stack are equal, not equal, then the stack continues until all elements of the stack, the stack of the last element is determined is empty, if empty, returns true, return false is not empty

Code:

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) 
    {
        stack<int> v;
        int j=0;
        if(pushV.empty()||popV.empty()||pushV.size()!=popV.size())
            return false;
        for(int i=0;i<pushV.size();i++)
        {
              v.push(pushV[i]);
              while(j<popV.size()&&v.top()==popV[j])
             {
                v.pop();
                j++;
             }
        }
        if(!v.empty())
        {
            return false;
        }
        return true;

        
    }
};
Published 42 original articles · won praise 13 · views 1764

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/105278124