-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.15.py
executable file
·45 lines (39 loc) · 1.19 KB
/
4.15.py
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
44
45
class NestedInteger(object):
def __init__(self, value=None):
"""
If value is not specified, initializes an empty list.
Otherwise initializes a single integer equal to value.
"""
def __call__(self, a):
return a
class Solution:
def deserialize(self, s: str) -> NestedInteger:
index = 0
def dfs() -> NestedInteger:
nonlocal index
if s[index] == '[':
index += 1
ni = NestedInteger()
while s[index] != ']':
ni.add(dfs())
if s[index] == ',':
index += 1
index += 1
return ni
else:
negative = False
if s[index] == '-':
negative = True
index += 1
num = 0
while index < len(s) and s[index].isdigit():
num *= 10
num += int(s[index])
index += 1
if negative:
num = -num
return NestedInteger(num)
return dfs()
a = Solution()
b = a.deserialize('324')
print(b(1))