package leetcode;
import java.util.Stack;
public class offer_31 {
public boolean validateStackSequences(int[] pushed, int[] popped) {
//用一个栈模拟进栈顺序
Stack<Integer> stack=new Stack<Integer>();
int i=0;
for(int num:pushed) {
stack.push(num);
//如果当前栈顶等于poped[i],则说明要弹出栈顶元素
while(!stack.isEmpty()&&stack.peek()==popped[i]) {
stack.pop();
i=i+1;
}
}
//根据最后栈是否为空判断是否为出栈顺序
return stack.isEmpty();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
offer_31 off=new offer_31();
int[] pushed= {4,0,3,1,2};
int[] poped= {3,1,0,2,4};
System.out.println(off.validateStackSequences(pushed, poped));
}
}