1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { int index = 0; Stack<Integer> stack = new Stack<>(); for (int num : pushed) { stack.push(num);
while (!stack.isEmpty() && popped[index] == stack.peek()) { stack.pop(); index++; } }
return stack.isEmpty(); } }
|
References
946. Validate Stack Sequences
剑指 Offer 31. 栈的压入、弹出序列