【LeetCode】385. Mini Parser 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/mini-parser/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:

  1. String is non-empty.
  2. String does not contain white spaces.
  3. 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.

题目大意

给了一个字符串表示的列表,返回一个NestedInteger形式的列表,其每个元素都是NestedInteger形式的整数对象。

面向对象的编程。

解题方法

Python看到这个题笑了,直接eval就能把字符串转成一个数组,而且这个数组的每个元素已经转成了int。

直接写个递归就好了。哈哈哈

# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger(object):
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """"[123,[456,[789]]]"
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """ class Solution(object):
def deserialize(self, s):
"""
:type s: str
:rtype: NestedInteger
"""
def getNumber(nums):
if isinstance(nums, int):
return NestedInteger(nums)
lst = NestedInteger()
for num in nums:
lst.add(getNumber(num))
return lst
return getNumber(eval(s))

日期

2018 年 3 月 13 日

【LeetCode】385. Mini Parser 解题报告(Python)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. 385. Mini Parser - LeetCode

    Question 385. Mini Parser Solution 分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]过程如下: # ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. Redis学习小结

    在7月中旬,我成功入职实习,通过进入公司,认识到了个人与企业巨大的差距,首先就是对于中间件的使用,ElasticSearch.Redis.Kafka等等,都是听过却从未使用过的,然而在任务下达之后,激 ...

  2. Redis | 第9章 Lua 脚本与排序《Redis设计与实现》

    目录 前言 1. Lua 脚本 1.1 Redis 创建并修改 Lua 环境的步骤 1.2 Lua 环境协作组件 1.3 EVAL 命令的实现 1.4 EVALSHA 命令的实现 1.5 脚本管理命令 ...

  3. C#表头固定

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="info.ascx.cs& ...

  4. 日常Java 2021/9/21

    将Java数组中的元素前后反转.题目要求:已知一个数组arr = {11,12,13,14,15}用程序实现把该数组中的元素值交换,交换后的数组arr = { 15,14,13,12,11},并输出交 ...

  5. day13 装饰器与语法糖

    day13 装饰器与语法糖 一.装饰器 1.什么是装饰器 装饰器就是装饰别人的工具,具体是指为被装饰者添加新功能 装饰器->函数 被装饰者->函数 2.为何要用装饰器 装饰器的核心思想:( ...

  6. day29并发编程

    day29并发编程 1.进程锁 一.使用锁维护执行顺序 代码: from multiprocessing import Process,Lock import os import time def t ...

  7. [JAVA]动态代理与AOP的千丝万缕

    动态代理与AOP的联系 别的不说,直接上图 首先是AOP切面编程 什么是切面?(自己心里想想就ok)所以所谓的切面编程,你也就懂得大体了,只是这个被切的是个程序而已 那么AOP与动态代理有什么关系呢? ...

  8. HttpClient连接池设置引发的一次雪崩

    事件背景 我在凤巢团队独立搭建和运维的一个高流量的推广实况系统,是通过HttpClient 调用大搜的实况服务.最近经常出现Address already in use (Bind failed)的问 ...

  9. HelloWorldModelMBean

    package mbeanTest; import java.lang.reflect.Constructor; import javax.management.Descriptor; import ...

  10. Output of C++ Program | Set 12

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...