您的位置:首页 > Web前端

剑指offer 22 栈的压入、弹出序列

2016-05-23 09:53 211 查看
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()函数,会发生未知!

*要注意边界条件!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: