This is an easy parser that can support nested list, link:https://leetcode.com/problems/mini-parser/description/
Description
Given a nested list of integers represented as a string, implement a parser to deserialize it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.
Note: You may assume that the string is well-formed:
- String is non-empty.
- String does not contain white spaces.
- String contains only digits
0-9
,[
,-
,
,]
.
Example 1:
1 | Given s = "324", |
Example 2:
1 | Given s = "[123,[456,[789]]]", |
Implementation
We use devide and conquer to parse it.
1 | # """ |
0 | i1 | n2 | t3 | e4 | N5 | ||||
---|---|---|---|---|---|---|---|---|---|
e | 1 | 2 | 3 | 3 | 4 | ||||
x | 2 | 2 | 3 | 4 | 4 | ||||
e | 3 | 3 | 3 | 3 | 4 | ||||
c | 4 | 4 | 4 | 4 | 4 | ||||
u | 5 | 5 | 5 | 5 | 5 | ||||