lt.946. 验证栈序列
[案例需求]
[思路分析]
[代码实现]
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
//pushed先出栈, 遇到poped的相同元素一起出栈, 然后再把pop完全出栈;
Deque<Integer> stack = new LinkedList<>();
int i = 0;
for(int num : pushed){
stack.push(num);
while(!stack.isEmpty() && stack.peek() == popped[i]){
stack.pop();
++i;
}
}
return stack.isEmpty();
}
}
具体题解: 点我