剑指offer 面试题22 栈的压入弹出序列

来源:互联网 发布:手机版java通用版 编辑:程序博客网 时间:2024/06/08 17:14


#include <iostream>#include <stack>using namespace std;bool IsPopOrder(const int *pPush,const int *pPop,int len){bool result=false;if(pPush==NULL||pPop==NULL||len<=0)return result;std::stack<int> DataStack;const int *pPushNext=pPush;const int *pPopNext=pPop;while(pPopNext-pPop<len){//push elementwhile(DataStack.empty()||DataStack.top()!=*pPopNext){//pLast-pFirst==len-1!if(pPushNext-pPush==len)break;DataStack.push(*pPushNext);pPushNext++;}if(DataStack.top()!=*pPopNext)break;//pop element after checkDataStack.pop();pPopNext++;}if(DataStack.empty()&&pPopNext-pPop==len)result=true;return result;}int main(){int a[]={1,2,3,4,5};int b[]={4,5,3,2,1};int c[]={4,3,5,1,2};cout<<IsPopOrder(a,b,5)<<IsPopOrder(a,c,5);}


0 0
原创粉丝点击