问题
例子
思路
-
方法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