输入两个整数序列,第一个序列表示栈的压入顺序,判断第二个序列是否为该栈
在这里插入代码片
```
class Test{
public static boolean isPopArray(int[]pushArray,int[]popArray){
if(popArray==null||popArray.length<0||
pushArray==null|| pushArray.length<0){
return false;
}
Stack<Integer> stack=new Stack<>();
int j=0;
for(int i=0;i<pushArray.length;i++){
stack.push(pushArray[i]);
while(!stack.isEmpty()&&stack.peek()==popArray[j]){
stack.pop();
j++;
}
}
if(stack.empty()){
return true;
}
System.out.println("Given sequence is not a pop-stack sequence");
return false;
}
public static void main(String[] args) {
int[] pushArray={1,2,3,4,5};
int[] popArray1={5,4,3,2,1};
int[] popArray2={4,3,5,2,1};
int[] popArray3={3,4,1,2,5};
int[] popArray4={5,3,4,2,1};
int[] popArray6={4,3,2,5,1};
System.out.println(isPopArray(pushArray,popArray1));
System.out.println(isPopArray(pushArray,popArray2));
System.out.println(isPopArray(pushArray,popArray3));
System.out.println(isPopArray(pushArray,popArray4));
System.out.println(isPopArray(pushArray,popArray6));
}