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()
相关推荐
绘梨衣5475 分钟前
异步任务队列-学习2
爬虫·python·学习·任务队列
用户8356290780511 小时前
使用 Python 在 Excel 中插入 OLE 对象
后端·python
one3121 小时前
高精度MEMS电容式倾角传感器:原理、结构与Python三维可视化
开发语言·python
重生之后端学习2 小时前
53. 最大子数组和[中等]✅
java·数据结构·算法·leetcode·职场和发展
深蓝海拓2 小时前
基于QtPy (PySide6) 的PLC-HMI工程实战记录(七)创建适合HMI项目的数字输入/显示类部件
python
MartinYeung52 小时前
[论文学习]MPIB:医疗提示注入基准——针对LLM临床安全性的系统性评估
人工智能·python·学习
用户8356290780512 小时前
使用 Python 为 PowerPoint 演示文稿添加评论
后端·python
阿图灵3 小时前
OpenCV 绘图五件套:画圆、文本、线段、矩形与椭圆
图像处理·人工智能·python·opencv·计算机视觉·绘图
web行路人3 小时前
AI全栈之旅 · 第一周总结
python