import java.util.ArrayList; import java.util.Stack; //一定记得加这个包 public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { if(pushA.length==0 || popA.length==0){ return false; } //辅助栈 Stack<Integer> s=new Stack<>(); //popA的指针 int popAIndex=0; for(int i=0;i<pushA.length;i++){ s.push(pushA[i]); //当辅助栈不为空且辅助栈的栈顶元素等于popA中指针所指的元素时,弹出s栈顶元素并将popA往后指一个 while(!s.isEmpty() && s.peek()==popA[popAIndex]){ s.pop(); popAIndex++; } } return s.isEmpty(); } }
原文:https://www.cnblogs.com/chanaichao/p/10170405.html