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();
    }
};

 

相关文章:

  • 2021-06-20
  • 2021-12-24
  • 2022-12-23
  • 2021-07-26
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-18
  • 2021-12-30
  • 2022-02-23
  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案