leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0946.cpp (673B)


      1 class Solution {
      2   public:
      3     bool validateStackSequences(vector<int> &pushed, vector<int> &popped) {
      4         int n = pushed.size(), m = popped.size();
      5         int i = 0, j = 0;
      6         stack<int> st;
      7 
      8         while (i < n || j < m) {
      9             if (st.empty()) {
     10                 if (i < n)
     11                     st.push(pushed[i++]);
     12                 else
     13                     return false;
     14             } else {
     15                 if (st.top() == popped[j])
     16                     st.pop(), j++;
     17                 else if (i < n)
     18                     st.push(pushed[i++]);
     19                 else
     20                     return false;
     21             }
     22         }
     23 
     24         return true;
     25     }
     26 };