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()
相关推荐
团子和二花9 分钟前
openclaw平替之nanobot源码解析(七):Gateway与多渠道集成
python·gateway·agent·智能体·openclaw·nanobot
未知鱼15 分钟前
Python安全开发之简易目录扫描器(含详细注释)
开发语言·python·安全
Be1k019 分钟前
推荐一款语雀知识库批量导出工具
python·gui·工具·语雀·批量导出·原创
Sunshine for you1 小时前
如何用FastAPI构建高性能的现代API
jvm·数据库·python
阿贵---1 小时前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
Red丶哞2 小时前
内网自建Postfix使用Python发送邮件
开发语言·python
rebekk2 小时前
pytorch custom op的简单介绍
人工智能·pytorch·python
chushiyunen2 小时前
uv使用笔记(python包的管理工具)
笔记·python·uv
曲幽2 小时前
FastAPI状态共享秘籍:别再让中间件、依赖和路由“各自为政”了!
python·fastapi·web·request·state·depends·middleware
风清扬【coder】2 小时前
Anaconda 被误删后抢救手册:数据恢复 + 环境重建应急流程
python·数据恢复·anaconda·环境重建