题目:
题解:
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()