946. 验证栈序列

执行用时:1 ms, 在所有 Java 提交中击败了92.86% 的用户
内存消耗:41 MB, 在所有 Java 提交中击败了36.08%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque<Integer> deque = new ArrayDeque<>();
int i = 0;
for (int val: pushed) {
deque.addLast(val);
while (!deque.isEmpty() && deque.peekLast() == popped[i]) {
deque.pollLast();
i ++;
}
}
return deque.isEmpty();
}
}

946. 验证栈序列
https://pisces34.github.io/2022/03/16/leetcode/946/
发布于
2022年3月16日
许可协议