面试题14 栈的 push, pop 序列 【栈】

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#define BUG cout << "here\n";
using namespace std;
const int N = 105;
/// 1 2 3 4 5 pNextPush
/// 4 3 5 1 2 pNextPop
bool isPopOrder(int* pPush, int* pPop, int len) {
    bool flag = false;
    if(pPush != NULL && pPop != NULL && len > 0) {
        const int* pNextPush = pPush;
        const int* pNextPop = pPop;
        stack<int> stackData;
        while(pNextPop - pPop < len) {
            while(stackData.empty() || stackData.top() != *pNextPop) {
                if(pNextPush - pPush == len) break;
                stackData.push(*pNextPush);
                pNextPush++;
            }
            if(stackData.top() != *pNextPop) break;
            stackData.pop();
            pNextPop++;
        }
        if(pNextPop - pPop == len) flag = true;
    }
    return flag;
}
int main() {
    return 0;
}

原文地址:https://www.cnblogs.com/robbychan/p/3787142.html