public static boolean isPopOrder01(int[] pushA, int[] popA) { if (pushA.length == 0 || popA.length == 0) { return false; } int popIndex = 0; Stack<Integer> stack = new Stack<>(); for (int num : pushA) { stack.push(num); while (!stack.isEmpty() && stack.peek().equals(popA[popIndex])) { stack.pop(); popIndex++; } } System.out.println(stack); return stack.isEmpty(); }
public static void main(String[] args) { int[] pushA = {1, 2, 3, 4, 5}; int[] popA = {5, 4, 3, 1, 2}; System.out.println(isPopOrder01(pushA, popA)); }