思路:如果下一个弹出的数字刚好是栈顶数字,则直接弹出。若下一个弹出的数字不在栈顶,则把压栈序列中还没有入栈的数字压入辅助栈,直到把下一个需要弹出的数字压入栈顶为止。若所有的数字都压入栈了仍没有找到下一个弹出的数字,则表明该序列不可能滴一个弹出序列。
代码:
#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;
bool IsPopOrder(int *pPush, int *pPop, int nLength)
{
if (pPush == NULL || pPop == NULL || nLength <= 0)
{
return false;
}
stack<int> s;
s.push(pPush[0]);
int nPop_index = 0;
int nPush_index = 1;
while (nPop_ind