题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
用一个栈模拟:
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer> s = new Stack();
int i = 0 , j = 0;
while(i < pushA.length){
if(pushA[i] == popA[j]){
i++;
j++;
while(!s.isEmpty() && popA[j] == s.peek()){//看有没有符合加入就弹出
s.pop();
j++;
}
}else{
s.push(pushA[i]);
i ++;
}
}
return s.isEmpty();
}
}