Paste_Image.png
public static boolean solution(int[] push,int pop[]){
if(push.length==0||pop.length==0||push.length!=pop.length) return false;
int index=0;
Stack s=new Stack;
for(int i=0;i<push.length;i++){
//push一個元素進(jìn)去
s.push(push[i]);
//如果元素相同就彈出,如果元素不相同,繼續(xù)壓入下一個元素進(jìn)去困檩。然后繼續(xù)比較
while(!s.empty()&&s.peek()==pop[index]){
s.pop();
index++;
}
}
//如果最后是空的就是正確的
return s.empty()
}