1.即使是用JAVA给的Stack类,也要注意栈的判空问题,因为库函数Pop()是会因为栈空抛出异常的,所以要处理一下判栈空的问题,具体仔细看代码,另外要记得不要调换条件判断顺序,切记&&的左右先后作用哦!
2.永远不要忘记对函数参数进行是否符合题意的判断;
3.另外记得这道题并没有默认两个数组的元素都是同样的数值范围,所以结尾的栈空的判断不可少。
上代码:
import java.util.ArrayList;
import java.util.Stack;
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>s=new Stack<>();
int length=pushA.length;
int j=0;
for(int i=0;i<length;i++){
if(popA[j]==pushA[i]){
j++;
while(!s.empty()&&s.peek()==popA[j]){
s.pop();
j++;
}
if(!s.empty()&&s.search(popA[j])!=-1){
return false;
}
}else{
s.push(pushA[i]);
}
}
if(s.empty())return true;
else{
return false;
}
}
}
京公网安备 11010502036488号