思路
1.先遍历压栈,遇到可弹出的先弹出
2.遍历完成后,全部弹出
3.未能完全弹出,则错误完全弹出,则正确
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
int len = pushV.size();
stack<int> test;
if(len==0)
return true;
int i =0;
int j =0;
for(;j<len;j++)//在遍历压栈时,遇到可弹出的先弹出
{
if(pushV[j]==popV[i])
{
i++;
continue;
}
else
{
test.push(pushV[j]);
}
}
while(!test.empty()&&test.top()==popV[i])//遍历完成后,全部弹出
{
test.pop();
i++;
}
if(i!=len)//若未能完全弹出,则false
return false;
return true;
}
};


京公网安备 11010502036488号