这道题其实也很简单。因为没有相等的数字。
所以碰见相等就出栈。不等就入栈就行。最后还有元素,序列就肯定不对。
import java.util.*;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(null == pushA){
return true;
}
Stack<Integer> stack = new Stack<>();
int index = 0;
for(int i=0;i<pushA.length;i++){
stack.push(pushA[i]);
while(!stack.empty() && stack.peek() == popA[index]){
stack.pop();
index++;
}
}
return stack.empty();
}
}