本文主要是介绍Python | Leetcode Python题解之第385题迷你语法分析器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution:def deserialize(self, s: str) -> NestedInteger:index = 0def dfs() -> NestedInteger:nonlocal indexif s[index] == '[':index += 1ni = NestedInteger()while s[index] != ']':ni.add(dfs())if s[index] == ',':index += 1index += 1return nielse:negative = Falseif s[index] == '-':negative = Trueindex += 1num = 0while index < len(s) and s[index].isdigit():num *= 10num += int(s[index])index += 1if negative:num = -numreturn NestedInteger(num)return dfs()
这篇关于Python | Leetcode Python题解之第385题迷你语法分析器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!