1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Deque<Integer> stack = new LinkedList<>(); boolean[] flag = new boolean[pushed.length];
for (int pop : popped) { for (int i = 0; i < pushed.length; i++) { if (!flag[i]) { stack.push(pushed[i]); flag[i] = true; }
if (!stack.isEmpty() && stack.peek() == pop) { stack.pop(); break; } } } return stack.isEmpty(); } }
|