Skip to content

Commit 3cb0d51

Browse files
authored
Merge pull request #3171 from a1exanddrovich/0946-dev
Create: 0946-validate-stack-sequence.java
2 parents 64448e6 + 15d2322 commit 3cb0d51

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)