Python | Leetcode Python题解之第385题迷你语法分析器

题目:

题解:

python 复制代码
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()
相关推荐
yaoh.wang6 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
小鸡吃米…6 小时前
Python PyQt6教程七-控件
数据库·python
1916zz7 小时前
Extreme programing 方利喆 _ 江贤晟
python
长安牧笛7 小时前
智能鞋柜—脚气终结者,内置温湿度传感器和紫外线灯,晚上回家,把鞋放进去,自动检测湿度,湿度超标就启动烘干+紫外线杀菌,第二天穿鞋干燥无异味。
python
weixin_457760007 小时前
PIL库将图片位深度是1、8、32统一转换为24的方法
python
Lucky高8 小时前
Pandas库入门
python·pandas
小鸡吃米…9 小时前
Python PyQt6教程三-菜单与工具栏
开发语言·python
sin_hielo9 小时前
leetcode 2110
数据结构·算法·leetcode
Jack电子实验室9 小时前
【杭电HDU】校园网(DeepL/Srun)自动登录教程
python·嵌入式硬件·计算机网络·自动化
木头左9 小时前
二值化近似计算在量化交易策略中降低遗忘门运算复杂度
python