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()
相关推荐
小白兔奶糖ovo几秒前
【Leetcode】231. 2的幂
linux·算法·leetcode
安替-AnTi7 分钟前
厚朴 APK 搜索接口分析
python·apk·解析·taobao
山川湖海31 分钟前
AI时代快速学编程语言的陷阱(以Python为例)
大数据·人工智能·python
H Journey34 分钟前
Supervisor 进程管理工具介绍
python·supervisor·linux 运维
过期动态1 小时前
【LeetCode 热题 100】接雨水
java·数据结构·算法·leetcode·职场和发展
春日见1 小时前
5分钟入门强化学习之动态规划算法与实现
大数据·人工智能·python·算法·机器学习·计算机视觉
DeniuHe2 小时前
sklearn 中所有交叉验证数据集划分方式完整总结
人工智能·python·sklearn
DeniuHe2 小时前
sklearn中不同交叉验证方法的场景适配
人工智能·python·sklearn
隐于花海,等待花开2 小时前
16.Python 常用第三方库概览 深度解析
python
我材不敲代码2 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python