0%
题解方法
栈
模拟入栈、出栈过程。
- do 访问 $num \in pushed$:
- num 入栈。
- do 出栈 While 栈顶元素 = popped[i]:
- 出栈。
- i++。
- 判断栈是否为空。
核心代码
栈
1 2 3 4 5 6 7 8 9 10 11 12
| public boolean validateStackSequences(int[] pushed, int[] popped) { Deque<Integer> stack = new ArrayDeque<Integer>(); int i = 0; for (int num: pushed) { stack.push(num); while (!stack.isEmpty() && stack.peek() == popped[i]) { stack.pop(); i++; } } return stack.isEmpty(); }
|
题目来源
栈的压入、弹出序列 - 力扣(LeetCode)