import java.util.Stack;
public class Test21 {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 4, 5, 3, 2, 1 };
int[] c = { 4, 3, 5, 1, 2 };
int[] d = { 3, 5, 4, 2, 1 };
System.out.println(IsPopOrder(a, b));
System.out.println(IsPopOrder(a, c));
System.out.println(IsPopOrder(a, d));
}
public static boolean IsPopOrder(int[] pushA, int[] popA) {
if pushA == null || popA == null ||
pushA.length == 0 || popA.length == 0 ||
pushA.length != popA.length) {
return false;
}
Stack<Integer> stack = new Stack<>();
int m = 0, n = 0;
stack.push(pushA[m++]);
while (n < popA.length ) {
while (popA[n] != stack.peek()) {
if (m == pushA.length) {
return false;
}
stack.push(pushA[m++]);
}
n++;
stack.pop();
}
return true;
}
}