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()
相关推荐
飞翔的佩奇5 分钟前
【完整源码+数据集+部署教程】食品分类与实例分割系统源码和数据集:改进yolo11-AggregatedAttention
python·yolo·计算机视觉·数据集·yolo11·食品分类与实例分割
火车叨位去194915 分钟前
力扣top100(day04-05)--堆
算法·leetcode·职场和发展
OperateCode21 分钟前
AutoVideoMerge:让二刷更沉浸的自动化视频处理脚本工具
python·opencv·ffmpeg
蔡俊锋21 分钟前
Javar如何用RabbitMQ订单超时处理
java·python·rabbitmq·ruby
跟橙姐学代码29 分钟前
学Python别死记硬背,这份“编程生活化笔记”让你少走三年弯路
前端·python
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 11: 寻找峰值、山脉数组的峰顶索引
java·算法·leetcode
站大爷IP1 小时前
Python与MySQL:从基础操作到实战技巧的完整指南
python
老歌老听老掉牙1 小时前
SymPy 矩阵到 NumPy 数组的全面转换指南
python·线性代数·矩阵·numpy·sympy
站大爷IP2 小时前
Python条件判断:从基础到进阶的实用指南
python
赛博郎中2 小时前
pygame小游戏飞机大战_8继承精灵玩家优化
python·pygame