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()
相关推荐
如何原谅奋力过但无声12 分钟前
【chap11-动态规划(上 - 基础题目&背包问题)】用Python3刷《代码随想录》
数据结构·python·算法·动态规划
云姜.29 分钟前
JSON Schema使用
python·json
Sunshine for you36 分钟前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
qwehjk200839 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
小熊Coding1 小时前
重庆市旅游景点数据可视化分析系统
爬虫·python·数据挖掘·数据分析·计算机毕业设计·数据可视化分析·旅游景点
sg_knight1 小时前
CentOS 裸机实操:5分钟完成 MinIO 单机部署与公网访问
linux·python·centos·文件管理·minio·ftp·oss
rebekk1 小时前
PyTorch Dispatcher介绍
人工智能·pytorch·python
呱牛do it1 小时前
企业级软件研发团队绩效考核系统开发(持续更新 Day 8)
python·fastapi·研发管理
阿kun要赚马内1 小时前
Python面向对象:@property装饰器
开发语言·前端·python
测试19981 小时前
Python+Excel读取和存储测试数据完成接口自动化测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试