一. 思路

直接按照自己如何解题的思路来。

核心是扫描一遍入栈数组并将数组元素入栈。匹配到栈元素与出栈数组中的元素相同时,就出栈并再次循环判断。

二. 代码

import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        boolean isPopOrder = false;
        Stack<Integer> stack = new Stack<Integer>();
        int j = 0;

        for (int i = 0; i < pushA.length; i++){
            stack.push(pushA[i]);

            while (!stack.isEmpty() && stack.peek() == popA[j]) {
                stack.pop();
                j++;
            }
        }

        return stack.isEmpty();
    }
}