forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
33 lines (33 loc) · 926 Bytes
/
Copy paths2.cpp
File metadata and controls
33 lines (33 loc) · 926 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
// OJ: https://leetcode.com/problems/mini-parser/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(H) where H is the maximum depth of the output
class Solution {
private:
NestedInteger deserialize(string s, int &i) {
NestedInteger ni;
if (i >= s.size()) return ni;
if (s[i] != '[') {
int n = 0, sign = 1;
if (s[i] == '-') {
sign = -1;
++i;
}
while (i < s.size() && isdigit(s[i])) n = n * 10 + s[i++] - '0';
ni.setInteger(sign * n);
} else {
++i;//[
while (i < s.size() && s[i] != ']') {
ni.add(deserialize(s, i));
if (i < s.size() && s[i] == ',') ++i;
}
++i;//]
}
return ni;
}
public:
NestedInteger deserialize(string s) {
int i = 0;
return deserialize(s, i);
}
};