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