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()
相关推荐
zhiSiBuYu051740 分钟前
Python3 模块开发与应用实战指南
python
databook1 小时前
用方差阈值过滤掉“惰性特征”
python·机器学习·scikit-learn
Freak嵌入式1 小时前
MCU 低功耗模式解析:时钟门控、电源门控、深度休眠
人工智能·python·单片机·嵌入式硬件·开源·依赖倒置原则·micropython
小poop2 小时前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
weixin_BYSJ19873 小时前
springboot校园自习室管理小程序---附源码32142
java·javascript·spring boot·python·django·flask·php
用户8356290780513 小时前
使用 Python 在 PDF 中绘制线条、矩形和自定义图形
后端·python
wang_yb3 小时前
用方差阈值过滤掉“惰性特征”
python·ai·databook
威联通网络存储3 小时前
TS-h2490FU在钢铁热轧/冷轧板材表面缺陷在线视觉检测中的部署
人工智能·python·计算机视觉·视觉检测
RFID固定资产管理系统5 小时前
公司RFID管理系统揭秘
大数据·python
IVEN_5 小时前
Python官方包、Conda、uv,应该怎么选
python