【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. kubernetes 用到的工具及组件

    kubernetes 用到的工具及组件,将所有的组件下载后放到/usr/local/bin目录下(记得chmod a+x /usr/local/bin/*).所有的组件,原则上都用最新的,如果遇到不支 ...

  2. Linux驱动实践:如何编写【 GPIO 】设备的驱动程序?

    作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...

  3. springcloud - alibaba - 2 - 集成Feign - 更新完成

    1.依赖 依赖管理 <parent> <artifactId>spring-boot-parent</artifactId> <groupId>org. ...

  4. 巩固javaweb第八天

    巩固内容: HTML 段落 HTML 可以将文档分割为若干段落. HTML 段落 段落是通过 <p> 标签定义的. 实例 <p>这是一个段落 </p> <p& ...

  5. Hadoop入门 概念

    Hadoop是分布式系统基础架构,通常指Hadoop生态圈 主要解决 1.海量数据的存储 2.海量数据的分析计算 优势 高可靠性:Hadoop底层维护多个数据副本,即使Hadoop某个计算元素或存储出 ...

  6. 逻辑学与Prolog学习笔记

    int a = 3 + 5; 很自然.如果Matrix a, b要加呢?没有运算符重载,a + b是不行的,只能add(a, b). int a = add(3, 5)也行.如果函数名可以用+呢?+( ...

  7. Shell【常用知识总结】

    一.常用知识总结 1.特殊变量($0,@,#,*,?) $0:当前脚本的文件名. $n:n是一个数字,表示第几个参数. $#:传递给脚本或函数的参数个数. $*:传递给脚本或函数的所有参数.当被双引号 ...

  8. CentOS7 搭建maven私服Nexus

    下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...

  9. 容器之分类与各种测试(三)——stack

    stack是栈,其实现也是使用了双端队列(只要不用双端队列的一端,仅用单端数据进出即完成单端队列的功能),由于queue和stack的实现均是使用deque,没有自己的数据结构和算法,所以这俩也被称为 ...

  10. vim中搜索指定单词(不加前后缀)

    \< : 搜索内容作为单词开头 \> : 搜索内容作为单词结尾 一起用即为将搜索内容指定为whole word e.g. : word_suffix word 如果用/word来搜索则两个 ...