LeetCodeAnimation/0946--validate-stack-sequences/Code/1.java
2020-04-19 09:09:30 +08:00

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;
}
}