MCPcopy
hub / github.com/qiyuangong/leetcode / validateStackSequences

Method validateStackSequences

java/946_Validate_Stack_Sequences.java:2–22  ·  view source on GitHub ↗
(int[] pushed, int[] popped)

Source from the content-addressed store, hash-verified

1class Solution {
2 public boolean validateStackSequences(int[] pushed, int[] popped) {
3 Stack<Integer> inStack = new Stack<>();
4 int posPush = 0, posPop = 0;
5 while (posPush != pushed.length) {
6 int curr = pushed[posPush];
7 while (!inStack.empty() && popped.length > 0 && inStack.peek() == popped[posPop]) {
8 inStack.pop();
9 posPop++;
10 }
11 if (popped.length == 0) break;
12 if (curr == popped[posPop]) posPop++;
13 else inStack.push(curr);
14 posPush++;
15 }
16 while (!inStack.empty() && popped.length > 0 && inStack.peek() == popped[posPop]) {
17 inStack.pop();
18 posPop++;
19 }
20 if (inStack.empty()) return true;
21 return false;
22 }
23}

Callers

nothing calls this directly

Calls 4

emptyMethod · 0.95
popMethod · 0.95
pushMethod · 0.95
peekMethod · 0.45

Tested by

no test coverage detected