leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0946.cpp (673B)
0 class Solution { 1 public: 2 bool validateStackSequences(vector<int> &pushed, vector<int> &popped) { 3 int n = pushed.size(), m = popped.size(); 4 int i = 0, j = 0; 5 stack<int> st; 6 7 while (i < n || j < m) { 8 if (st.empty()) { 9 if (i < n) 10 st.push(pushed[i++]); 11 else 12 return false; 13 } else { 14 if (st.top() == popped[j]) 15 st.pop(), j++; 16 else if (i < n) 17 st.push(pushed[i++]); 18 else 19 return false; 20 } 21 } 22 23 return true; 24 } 25 };