LeetCode 385. Mini Parser

简介: 给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。列表中的每个元素只可能是整数或整数嵌套列表

v2-bc3f2db2039e50bb15b5a828b4386191_1440w.jpg

Description



Given a nested list of integers represented as a string, implement a parser to deserialize it.


Each element is either an integer, or a list -- whose elements may also be integers or other lists.


Note: You may assume that the string is well-formed:

String is non-empty.

String does not contain white spaces.

String contains only digits 0-9, [, - ,, ].


Example 1:


Given s = "324",
You should return a NestedInteger object which contains a single integer 324.


Example 2:


Given s = "[123,[456,[789]]]",
Return a NestedInteger object containing a nested list with 2 elements:
1. An integer containing value 123.
2. A nested list containing two elements:
    i.  An integer containing value 456.
    ii. A nested list with one element:
         a. An integer containing value 789.


描述



给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。

列表中的每个元素只可能是整数或整数嵌套列表


提示:你可以假定这些字符串都是格式良好的:

字符串非空

字符串不包含空格

字符串只包含数字0-9, [, - ,, ]


示例 1:


给定 s = "324",
你应该返回一个 NestedInteger 对象,其中只包含整数值 324。


示例 2:


给定 s = "[123,[456,[789]]]",
返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表:
    i.  一个 integer 包含值 456
    ii. 一个包含一个元素的嵌套列表
         a. 一个 integer 包含值 789

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/mini-parser

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路



  • 使用 json 将字符串转换成为 json 对象。
  • 递归地对 json 对象进行解析。


import json
from collections.abc import Iterable
class Solution:
    def deserialize(self, s: str):
        json_list = json.loads(s)
        def _deserialize(json_list):
            if isinstance(json_list, Iterable):
                nested = NestedInteger()
                for x in json_list:
                    nested.add(_deserialize(x))
                return nested
            else:
                return NestedInteger(json_list)
        return _deserialize(json_list)

源代码文件在 这里


目录
相关文章
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
26 6
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
26 4
|
12天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
39 2
|
12天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
15 7
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
12 4
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
31 5
|
12天前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
22 3
|
12天前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
11 3