面试题31. 栈的压入、弹出序列

题目:

 

 

解答:

 

 

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

 

posted @ 2020-05-09 15:30  梦醒潇湘  阅读(145)  评论(0)    收藏  举报