Skip to content

Commit 15d2322

Browse files
Create: 0946-validate-stack-sequence.java
Signed-off-by: Aliaksei <[email protected]>
1 parent 027cb6c commit 15d2322

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean validateStackSequences(int[] pushed, int[] popped) {
3+
Stack<Integer> stack = new Stack<>();
4+
5+
int i = 0;
6+
for (int value : pushed) {
7+
stack.push(value);
8+
9+
while (i < popped.length && !stack.isEmpty() && stack.peek() == popped[i]) {
10+
stack.pop();
11+
i++;
12+
}
13+
}
14+
15+
return stack.isEmpty();
16+
}
17+
}

0 commit comments

Comments
 (0)