LeetCodeAnimation/0946--validate-stack-sequences/Code/1.java

21 lines
549 B
Java
Raw Normal View History

2020-04-19 09:09:30 +08:00
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]) {
//<2F><>ͷԪ<CDB7>س<EFBFBD><D8B3>ӣ<EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD>س<EFBFBD>ջ
stack.pop();
j++;
}
}
if (!stack.isEmpty()){
return false;
}
return true;
}
}