首页 > 代码库 > [剑指Offer] 21.栈的压入、弹出序列

[剑指Offer] 21.栈的压入、弹出序列

 1 class Solution
 2 {
 3 public:
 4     bool IsPopOrder(vector<int> pushV,vector<int> popV) {
 5         if(pushV.size() != popV.size())
 6             return false;
 7         stack<int> Stack;
 8         int i = 0,j = 0;
 9         while(i < pushV.size())
10         {
11             Stack.push(pushV[i ++]);
12             //出栈队列中的元素和栈顶元素相等就出栈
13             while(j < popV.size() && popV[j] == Stack.top())
14             {
15                 Stack.pop();
16                 j ++;
17             }
18         }
19         return Stack.empty();//栈空则返回true
20     }
21 };

 

[剑指Offer] 21.栈的压入、弹出序列