AcWing 42. 栈的压入、弹出序列
原题链接
简单
作者:
joyonline
,
2019-12-17 08:41:17
,
所有人可见
,
阅读 935
java代码
class Solution {
public boolean isPopOrder(int [] pushV,int [] popV) {
int pul = pushV.length;
int pol = popV.length;
if(pul != pol) return false;
if(pul == 0 && pol == 0) return true;
Stack stack = new Stack();
int k = 0;
int flag = 0;
while(k < pul){
if(pushV[k] == popV[flag]){
k++;
flag++;
}else{
if(stack.isEmpty() || !stack.peek().equals(popV[flag])){
stack.push(pushV[k]);
k++;
}else{
stack.pop();
flag++;
}
}
}
if(stack.isEmpty()){
return true;
}
// for(int i=flag;i<pol;i++){
// // System.out.print(popV[i]);
// System.out.print(stack.pop());
// }
while(flag < pol){
if(!stack.pop().equals(popV[flag])){
return false;
}
flag++;
}
return true;
}
}