JZ31 栈的压入、弹出序列
栈的压入、弹出序列_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
import java.util.ArrayList;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA==null||popA==null||pushA.length==0||popA.length==0||pushA.length!=popA.length){
return false;
}
Stack<Integer> st=new Stack<>();
int i=0;
int j=0;
for(;i<pushA.length;i++){
st.push(pushA[i]);
while(!st.isEmpty()&&st.peek()==popA[j]){
st.pop();
j++;
}
}
//return i==j;
return st.isEmpty();
}
}