forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_946.java
30 lines (28 loc) · 975 Bytes
/
_946.java
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
package com.fishercoder.solutions;
import java.util.Stack;
public class _946 {
public static class Solution1 {
public boolean validateStackSequences(int[] pushed, int[] popped) {
if (pushed == null || popped == null || pushed.length == 0 || popped.length == 0) {
return true;
}
Stack<Integer> stack = new Stack<>();
stack.push(pushed[0]);
int i = 1;
int j = 0;
while (!stack.isEmpty() || j < popped.length) {
if (j < popped.length && !stack.isEmpty() && stack.peek() == popped[j]) {
stack.pop();
j++;
} else {
if (i < pushed.length) {
stack.push(pushed[i++]);
} else {
return stack.isEmpty();
}
}
}
return stack.isEmpty();
}
}
}