Python | Leetcode Python题解之第385题迷你语法分析器
题目:
题解:
class Solution:def deserialize(self, s: str) -> NestedInteger:if s[0] != '[':return NestedInteger(int(s))stack, num, negative = [], 0, Falsefor i, c in enumerate(s):if c == '-':negative = Trueelif c.isdigit():num = num * 10 + int(c)elif c == '[':stack.append(NestedInteger())elif c in ',]':if s[i-1].isdigit():if negative:num = -numstack[-1].add(NestedInteger(num))num, negative = 0, Falseif c == ']' and len(stack) > 1:stack[-2].add(stack.pop())return stack.pop()