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

栈的压入、弹出序列

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

public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        bool flag=true;
        stack<int> test;
        int i;
        int j=0;
        //使用暴力模拟的算法
        for(i=0;i<=pushV.size()-1;i+=1){
            while(!test.empty()&&test.top()==popV[j]){
                test.pop();
                j+=1;
            }
            
            if(pushV[i]==popV[j]){
                test.push(pushV[i]);
                test.pop();
                j+=1;
            }
            else{
                 test.push(pushV[i]); 
            } 
        }
        
        while(!test.empty()){
            if(test.top()!=popV[j]){
                flag=false;
            }
            test.pop();
            j+=1;
        }
        return flag;
    }
};
全部评论

相关推荐

本2硕9,秋招不想努力海投了
小何和:行情再不好也不可能拒绝你,不可能只要双9的
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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