模拟法,模拟栈的压入和弹出过程
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
// 模拟
int j=0;
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<pushed.length;i++){
stack.push(pushed[i]);
while(!stack.isEmpty() && stack.peek()==popped[j]){
stack.pop();
j++;
}
}
while(!stack.isEmpty()){
int temp=stack.pop();
if(temp!=popped[j]){
return false;
}
j++;
}
return true;
}
}