0%

剑指offer-31

题目

结果

代码

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) {
// Stack simulation
Deque<Integer> stack = new LinkedList<>();
// Check out if the num has been used
boolean[] flag = new boolean[pushed.length];

for (int pop : popped) {
for (int i = 0; i < pushed.length; i++) {
// If the num has not been used
if (!flag[i]) {
stack.push(pushed[i]);
flag[i] = true;
}

if (!stack.isEmpty() && stack.peek() == pop) {
stack.pop();
break;
}
}
}
return stack.isEmpty();
}
}