22栈的压入,弹出序列



bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
{
bool bPossible = false;
if (pPush != NULL&&pPop != NULL&&nLength > 0)
{
const int* pNextPush = pPush;
const int* pNextPop = pPop;
std::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;
}

阅读更多
文章标签:
个人分类: 剑指offer
博主设置当前文章不允许评论。

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭