用一个栈模拟:


public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
      Stack<Integer> s = new Stack();
        int i = 0 , j = 0;
        while(i < pushA.length){
            if(pushA[i] == popA[j]){
                i++;
                j++;
                while(!s.isEmpty() && popA[j] == s.peek()){//看有没有符合加入就弹出
                    s.pop();
                    j++;
                }
            }else{
                s.push(pushA[i]);
                i ++;
            }
        }
        return s.isEmpty();
    }
}