mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-06 15:19:44 +08:00
21 lines
549 B
Java
21 lines
549 B
Java
class Solution {
|
|
public boolean validateStackSequences(int[] pushed, int[] popped) {
|
|
|
|
int N = pushed.length;
|
|
Stack<Integer> stack = new Stack();
|
|
|
|
int j = 0;
|
|
for (int x: pushed) {
|
|
stack.push(x);
|
|
while (!stack.isEmpty() && j < N && stack.peek() == popped[j]) {
|
|
//队头元素出队,栈顶元素出栈
|
|
stack.pop();
|
|
j++;
|
|
}
|
|
}
|
|
if (!stack.isEmpty()){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
} |