剑指 Offer 31. 栈的压入、弹出序列
class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Stack<Integer> stack = new Stack<>(); int index = 0; for(int x : pushed){ stack.push(x); //while(popped[index] == stack.peek() && !stack.isEmpty()) 数组越界 while(!stack.isEmpty() && popped[index] == stack.peek() ){ stack.pop(); index++; } } return stack.isEmpty(); } }