栈的压入、弹出序列
题目 栈的压入、弹出序列
思路分析
不断进行压栈操作
如果发现栈顶元素与输出序列的当前比对位置相同 就出栈
最后如果是空 就说明是符合的
如果最后不是空 就说明不可能是该顺序
代码实现
class Solution {
public:
bool isPopOrder(vector<int> pushV,vector<int> popV) {
if(pushV.size()!=popV.size())
return false;
stack<int> stk;
int i=0;
for(auto x:pushV){
stk.push(x);
while(stk.size() && stk.top()==popV[i]){
stk.pop();
i++;
}
}
return stk.empty();
}
};
同类题型
视频讲解
⬅️ 最长合法括号子串 🏠 00-刷题理模型 ➡️ (待做)火车进出栈问题
💬 评论