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

栈的压入、弹出序列

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

class Solution {
public:
  //模拟压栈出栈的顺序
    bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
        stack<int> stk;

        int i=0;
        int n = popV.size();
        for(int a:pushV){
            stk.push(a);
            while( !stk.empty() && i<= n && stk.top() == popV[i] ){
                stk.pop();
                ++i;
            }
        }

        return (i == n)? true:false;
    }
};

全部评论

相关推荐

06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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