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()
相关推荐
TF男孩3 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在8 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP10 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805116 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
Fanxt_Ja16 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
c8i16 小时前
python中类的基本结构、特殊属性于MRO理解
python
liwulin050616 小时前
【ESP32-CAM】HELLO WORLD
python
Doris_202317 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_202317 小时前
Python 模式匹配match case
前端·后端·python
这里有鱼汤17 小时前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员