用一个queue存储弹出序列,在每次压栈之后循环对比并弹出栈顶与queue头,如果最后queue清空则为true
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pushV int整型一维数组
* @param popV int整型一维数组
* @return bool布尔型
*/
public static boolean IsPopOrder(int[] pushV, int[] popV) {
// write code here
boolean res = false;
Queue<Integer> q = new LinkedList<>();
Stack<Integer> stack = new Stack<>();
for (int popNum : popV) q.add(popNum);
for (int push : pushV) {
stack.push(push);
while (true) {
if (!stack.isEmpty() && Objects.equals(stack.peek(), q.peek())) {
stack.pop();
q.poll();
} else {
break;
}
}
}
if (q.isEmpty()) res = true;
return res;
}
}

京公网安备 11010502036488号