class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { bool result=false; int n1=pushV.size(); int n2=popV.size(); if(!n1||!n2) return result; int push_index=0; int pop_index=0; stack<int> s; s.push(pushV[push_index++]); while(pop_index<n2) { while(s.top()!=popV[pop_index]&&push_index<n1) s.push(pushV[push_index++]); if(s.top()==popV[pop_index]) { s.pop(); pop_index++; } else break; } if(pop_index==n2) result=true; return result; } };
*不可以在堆栈为空时,使用s.top()函数,会发生未知!
*要注意边界条件!!
时间: 2024-10-08 23:02:14