题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer>st = new Stack<Integer>();
int i=0,j=0;
while(j<popA.length && i<pushA.length){
while(j<popA.length && i<pushA.length && popA[j]!=pushA[i]){
st.add(pushA[i]);
i++;
}
if(i<pushA.length) st.add(pushA[i++]);
while(st.size()>0 && popA[j]==st.peek()){
st.pop();
j++;
}
}
if(st.size()==0) return true;
return false;
}
}