思路:
利用一个栈
代码:
自己写的第一版
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
if(pushed.length==0) return true;
Deque<Integer> stack=new ArrayDeque<>();
stack.addLast(pushed[0]);
int i=1,j=0;
while(i<pushed.length||!stack.isEmpty()){
if(j<popped.length&&stack.peekLast()!=null&&stack.peekLast().equals(popped[j])){
stack.removeLast();
j++;
}
else if(i<pushed.length){
stack.addLast(pushed[i++]);
}
else{
return false;
}
}
return true;
}
}
优化后的第二版
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque&