0
点赞
收藏
分享

微信扫一扫

剑指 Offer 31. 栈的压入、弹出序列

伢赞 2022-02-20 阅读 75
leetcode

思路:模拟就完事

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> c;
        int idx=0;
        for(int v:pushed){
            c.push(v);
            while(c.size()&&c.top()==popped[idx]){
                c.pop();
                idx++;
            }
        }
        while(c.size()){
            if(c.top()!=popped[idx]) return false;
            c.pop(); 
            idx++;
        }
        return true;
    }
};

 

举报

相关推荐

0 条评论