剑指offer C++ 栈的压入 弹出序列 挺绕的 对我来说很难

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if(pushV.empty() || popV.empty() || pushV.size() != popV.size()) return false;
        int pushP = 0;
        int popP = 0;
        stack<int> s;
        while(popP < popV.size()){
            while(s.empty() || s.top() != popV[popP]){
                if(pushP == pushV.size()) break;
                s.push(pushV[pushP]);
                pushP++;
            }
            if(s.top() == popV[popP]){
                s.pop();
                popP++;
            }
            else{
                return false;
            }
        }
        return (s.empty() && popP ==  popV.size());
    }
};

posted @ 2018-09-02 19:59  一条图图犬  阅读(254)  评论(0编辑  收藏  举报