题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
借助栈,边push边pop
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length!=popA.length)return false;
Deque st = new LinkedList();
int j=0;
for(int i=0;i<pushA.length;i++){
while(j<pushA.length&&(st.isEmpty()||st.peek()!=popA[i])){
st.push(pushA[j]);
j++;
}//当栈顶等于pop或者遍历完push都没找到pop元素时结束while循环
if(st.peek()==popA[i]){
st.pop();
}else{
return false;//说明push里没有pop[i]
}
}
return true;