- 946. 验证栈序列 中等">946. 验证栈序列 中等
946. 验证栈序列 中等
class Solution {public boolean validateStackSequences(int[] pushed, int[] popped) {LinkedList<Integer> stack = new LinkedList<>();int n = pushed.length;int index = 0;for (int i=0; i<n; i++) {int cur = popped[i];for ( ; index<=n; index++) {if (!stack.isEmpty() && stack.getLast() == cur) {stack.pollLast();break;}if (index < n) {stack.addLast(pushed[index]);}}}return stack.isEmpty();}}
