问题


例子

思路
-
方法1
$$$$
把pushed数组依次装入栈,同时让栈按照popped数组弹出元素,如果最终栈为空,则为true
-
方法2
$$$$
代码
//方法1
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> s = new Stack<>();
int i=0; //popped的下标
for(int n : pushed) {
s.push(n);
while(s.size()>0 && s.peek()==popped[i])
{
i++;
s.pop();
}
}
return s.size()==0;
}
}
//方法2

本文介绍了一种验证两个整数数组是否符合栈操作序列的方法。通过将pushed数组元素依次压入栈中,并尝试按popped数组顺序弹出,以此判断两数组是否匹配。提供了两种实现思路及代码示例。

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



