946. Validate Stack Sequences

 

946. Validate Stack Sequencesspa

 

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();
    }
};
相關文章
相關標籤/搜索