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