-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path946.java
More file actions
33 lines (33 loc) · 1.09 KB
/
946.java
File metadata and controls
33 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
int[] stack = new int[pushed.length];
int i = 0, top = -1;
for (int p : pushed) {
stack[++top] = p;
while (top > -1 && stack[top] == popped[i]) {
--top;
++i;
}
}
return top == -1;
}
}
__________________________________________________________________________________________________
sample 38108 kb submission
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stk = new Stack<>();
int i = 0;
for (int p : pushed) {
stk.push(p);
while (!stk.isEmpty() && stk.peek() == popped[i]) {
stk.pop();
++i;
}
}
return stk.empty();
}
}
__________________________________________________________________________________________________