#include<iostream>
#include<stack>
using namespace std;
bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
{
const int* pNextPush = pPush;
const int* pNextPop = pPop;
bool bPossible = false;
if (pPush != nullptr && pPop != nullptr && nLength > 0)
{
stack<int>stackData;
while (pNextPop - pPop < nLength)
{
while (stackData.empty() || stackData.top() != *pNextPop)
{
if (pNextPush - pPush == nLength)
break;
stackData.push(*pNextPush);
pNextPush++;
}
if (stackData.top() != *pNextPop)
break;
stackData.pop();
pNextPop++;
}
if (stackData.empty() && pNextPop - pPop == nLength)
bPossible = true;
}
return bPossible;
}
int main()
{
int a[] = {1,2,3,4,5};
int b[] = {4,5,3,1,2};
int nLength = 5;
bool result = IsPopOrder(a, b, nLength);
cout << result << endl;
cin.get();
return 0;
}
剑指offer 面试题31 栈的压入、弹出序列
最新推荐文章于 2024-10-05 11:02:16 发布