Loading

剑指 Offer 31. 栈的压入、弹出序列

思路

思路来源:https://leetcode-cn.com/problems/validate-stack-sequences/

 

 

 

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

 

posted @ 2020-10-28 16:03  拾月凄辰  阅读(62)  评论(0编辑  收藏  举报