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()
相关推荐
小李哥哥44 分钟前
基于数据的人工智能建模流程及源码示例
python
APIshop1 小时前
实战解析:苏宁易购 item_search 按关键字搜索商品API接口
开发语言·chrome·python
蓝桉~MLGT1 小时前
Python学习历程——Python面向对象编程详解
开发语言·python·学习
larance1 小时前
Python 中的 *args 和 **kwargs
开发语言·python
百锦再1 小时前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
yaoxin5211231 小时前
238. Java 集合 - 使用 ListIterator 遍历 List 元素
java·python·list
nvd112 小时前
python 后端流式处理 LLM 响应数据详解
开发语言·python
F_D_Z2 小时前
【解决办法】报错Found dtype Long but expected Float
人工智能·python
fanjinhong_85212 小时前
屏幕捕捉工具 (Screen Capture Tool)
python·github
做怪小疯子2 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法