【题目描述】
【思路】
可以发现序列的操作是唯一的
操作1:当栈stk为空时,stk只能压入元素。
操作2:当栈stk的栈顶元素与弹出序列元素相同时,stk必须弹出栈顶元素。
class Solution {
public boolean isPopOrder(int [] pushV,int [] popV) {
if( pushV.length != popV.length ) return false;
Stack<Integer> stk = new Stack<>();
int index = 0;//弹出序列指针
for(int i = 0; i < pushV.length; i ++){
stk.push(pushV[i]);
while( !stk.isEmpty() && stk.peek() == popV[index]){//元素值与弹出序列相同
//栈顶元素弹出
stk.pop();
index ++;
}
}
if( stk.isEmpty() ) return true;
return false;
}
}