剑指OFFER----面试题31. 栈的压入、弹出序列

链接:https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/

 

代码:

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        if (pushed.size() != popped.size()) return false;

        stack<int> res;
        int i = 0;
        for (auto x: pushed) {
            res.push(x);
            while (res.size() && res.top() == popped[i]) {
                res.pop();
                ++i;
            }
        }

        return res.empty();

    }
};

 

posted @ 2020-02-27 14:23  景云ⁿ  阅读(93)  评论(0编辑  收藏  举报