题目链接:
https://leetcode.cn/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/description/?favorite=xb9nqhhg&orderBy=hot
代码:
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int>cnt;
int j=0;
for(int i=0;i<pushed.size();i++)
{
cnt.push(pushed[i]);
while(!cnt.empty()&&popped[j]==cnt.top())
{
j++;
cnt.pop();
}
}
if(cnt.empty())
{
return true;
}
else return false ;
}
};
该问题讨论如何验证一个给定的push和pop操作序列是否能对应一个合法的栈操作过程。通过使用栈数据结构模拟push和pop操作,检查popped序列是否能按顺序匹配到pushed序列中的元素。
428

被折叠的 条评论
为什么被折叠?



