forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.cpp
32 lines (32 loc) · 1017 Bytes
/
s1.cpp
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
// OJ: https://leetcode.com/problems/mini-parser/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N^2)
class Solution {
public:
NestedInteger deserialize(string s) {
if (s.empty()) return NestedInteger(); // empty list
if (s[0] == '[') { // nested list
NestedInteger n;
int i = 1, end = s.size() - 1;
while (i < end) {
int begin = i;
if (s[i] == '[') { // element being nested list
int cnt = 0;
do {
if (s[i] == '[') ++cnt;
else if (s[i] == ']') --cnt;
++i;
} while (cnt > 0);
} else {
while (isdigit(s[i]) || s[i] == '-') ++i;
}
n.add(deserialize(s.substr(begin, i - begin)));
++i;
}
return n;
}
// plain number
return NestedInteger(stoi(s));
}
};