美文网首页
栈的压入弹出序列offer

栈的压入弹出序列offer

作者: BeijingIamback | 来源:发表于2016-08-18 14:05 被阅读17次
    bool isPopOrder(const int * pPush,const int * pPop, int nLength){
        bool res = false;
    
        if (pPush != nullptr && pPop != nullptr && nLength > 0){
            const int * pNextPush = pPush;
            const int * pNextPop = pPop;
            stack<int> stackData;
    
            while(pNextPop - pPop < nLength) {
                while(stackData.empty() || stackData.top() != *pNextPop) {
                    if(pNextPop - pPop == nLength)
                        break;
                    stackData.push(*pNextPush);
    
                    pNextPush++;
                }
                if(stackData.top() != *pNextPop)
                    break;
                stackData.pop();
                pNextPop++;
    
            }
            if(stackData.empty() && pNextPop - pPop == nLength)
                res = true;
        }
        return res;
    }
    

    相关文章

      网友评论

          本文标题:栈的压入弹出序列offer

          本文链接:https://www.haomeiwen.com/subject/zfuvsttx.html