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  有梦就要去实现他  阅读(...)  评论(...编辑  收藏