剑指offer(21)栈的压入弹出
package OfferTest;
//栈的压入,弹出序列
import java.util.Stack;
public class NoTwentyone {
public static boolean IsPopOrder(int[] pushA,int[] popA){
if(pushA == null || popA == null){
return false;
}
Stack<Integer> stack = new Stack<Integer>();
int curPop = 0;
for(int i = 0;i < pushA.length;i++){
stack.push(pushA[i]);
while(!stack.isEmpty() && stack.peek() == popA[curPop]){
stack.pop();
curPop++;
}
}
return stack.isEmpty();
}
public static void main(String[] args){
int[] pushA = new int[]{1,2,3,4,5};
int[] popA = new int[]{4,5,3,2,1};
boolean res = IsPopOrder(pushA,popA);
System.out.println(res);
}
}