946. Validate Stack Sequences

 

946. Validate Stack Sequences

 

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

 

posted @ 2019-08-06 17:40  有梦就要去实现他  阅读(172)  评论(0编辑  收藏  举报