Mini Parser LeetCode solution in JavaScript

1
var deserialize = function (s) {
2
if (s[0] !== "[") return new NestedInteger(s);
3

4
const stack = [];
5
let start = 0;
6
const process = {
7
"["(index) {
8
stack.push(new NestedInteger());
9
start = index + 1;
10
},
11
","(index) {
12
if (index > start) {
13
const value = s.slice(start, index);
14
stack[stack.length - 1].add(new NestedInteger(value));
15
}
16
start = index + 1;
17
},
18
"]"(index) {
19
this[","](index);
20
if (stack.length < 2) return;
21
const last = stack.pop();
22
stack[stack.length - 1].add(last);
23
},
24
};
25

26
[...s].forEach((current, index) => process[current]?.(index));
27

28
return stack[0];
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0