示例代码
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
int len = pushed.length;
Stack<Integer> stk = new Stack<>();
for (int i = 0, j = 0; i < len;i++) {
stk.push(pushed[i]);
while (!stk.isEmpty() && stk.peek() == popped[j]) {
stk.pop();
j++;
}
}
return stk.isEmpty();
}
}
效果展示