剑指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();

    }
};
原文地址:https://www.cnblogs.com/clown9804/p/12371970.html