题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
import java.util.*;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length == 0 || popA.length == 0 || pushA.length != popA.length)return false;
Stack<Integer> help = new Stack<Integer>();
int j = 0;//一开始就要初始化内循环的循环变量,后面要先用
for(int i = 0;i < pushA.length;i++){
help.push(pushA[i]);
while(!help.isEmpty() && help.peek() == popA[j]){
//这里注意逻辑的先后顺序,先保证辅助栈不为空在执行peek操作
help.pop();//执行辅助栈的弹栈
j++;//并且让j加一
}
}
return help.isEmpty();
}
}