forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMini Parser.cpp
More file actions
43 lines (36 loc) · 884 Bytes
/
Mini Parser.cpp
File metadata and controls
43 lines (36 loc) · 884 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
43
// Runtime: 20 ms (Top 36.02%) | Memory: 13.5 MB (Top 22.22%)
class Solution {
public:
NestedInteger deserialize(string s) {
int i = 0;
return helper(s, i).getList()[0];
}
NestedInteger helper(string &s, int &i)
{
NestedInteger nI;
while(i < s.size())
{
if(s[i] == ',')
{
i++;
continue;
}
if(s[i] == ']')
{
i++;
return nI;
}
if(s[i] == '[')
nI.add(helper(s, ++i));
else
{
string tmp;
while(i < s.size() && s[i] != ',' && s[i] != ']')
tmp += s[i++];
NestedInteger tmp_nI(stoi(tmp));
nI.add(tmp_nI);
}
}
return nI;
}
};