Title 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;
}
};