面试题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;
}

posted @ 2013-01-29 19:26  小尼人00  阅读(177)  评论(0)    收藏  举报