剑指offer 面试题31. 栈的压入、弹出序列 [中等]——贪心

面试题31. 栈的压入、弹出序列

贪心模拟出栈入栈

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        int m=pushed.size();
        int n=pushed.size();
        stack<int> s;
        int j=0;
        for(int i=0;i<m;i++){
            s.push(pushed[i]);
            while(!s.empty()&&j<n&&popped[j]==s.top()){
                s.pop();
                j++;
            }
        }
        return s.empty();
    }
};

猜你喜欢

转载自blog.csdn.net/qq_41041762/article/details/105883294