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()
相关推荐
风逸hhh4 分钟前
python打卡day58@浙大疏锦行
开发语言·python
烛阴1 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
JosieBook1 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
Gyoku Mint2 小时前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
郭庆汝8 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
Alfred king10 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
思则变11 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
漫谈网络11 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
try2find13 小时前
安装llama-cpp-python踩坑记
开发语言·python·llama
博观而约取14 小时前
Django ORM 1. 创建模型(Model)
数据库·python·django