题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        stack<int> s;
        int index=0;
        for(int i=0;i<popV.size();i++){
            if(pushV[index]==popV[i]){
                index++;
            }else{
                if(!s.empty()&&s.top()==popV[i]){
                    s.pop();
                }else{
                    while(pushV[index]!=popV[i]){
                        s.push(pushV[index]);
                        index++;
                    }
                    index++;
                }
            }
        }
        return s.empty();
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务