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.
详见:https://leetcode.com/problems/mini-parser/description/

C++:

  1. /**
  2. * // This is the interface that allows for creating nested lists.
  3. * // You should not implement it, or speculate about its implementation
  4. * class NestedInteger {
  5. * public:
  6. * // Constructor initializes an empty nested list.
  7. * NestedInteger();
  8. *
  9. * // Constructor initializes a single integer.
  10. * NestedInteger(int value);
  11. *
  12. * // Return true if this NestedInteger holds a single integer, rather than a nested list.
  13. * bool isInteger() const;
  14. *
  15. * // Return the single integer that this NestedInteger holds, if it holds a single integer
  16. * // The result is undefined if this NestedInteger holds a nested list
  17. * int getInteger() const;
  18. *
  19. * // Set this NestedInteger to hold a single integer.
  20. * void setInteger(int value);
  21. *
  22. * // Set this NestedInteger to hold a nested list and adds a nested integer to it.
  23. * void add(const NestedInteger &ni);
  24. *
  25. * // Return the nested list that this NestedInteger holds, if it holds a nested list
  26. * // The result is undefined if this NestedInteger holds a single integer
  27. * const vector<NestedInteger> &getList() const;
  28. * };
  29. */
  30. class Solution {
  31. public:
  32. NestedInteger deserialize(string s) {
  33. if(s.empty())
  34. {
  35. return NestedInteger();
  36. }
  37. if(s[0]!='[')
  38. {
  39. return NestedInteger(stoi(s));
  40. }
  41. if(s.size()<=2)
  42. {
  43. return NestedInteger();
  44. }
  45. NestedInteger res;
  46. int cnt=0;
  47. int start=1;
  48. for(int i=1;i<s.size();++i)
  49. {
  50. if(cnt==0&&s[i]==','||i==s.size()-1)
  51. {
  52. res.add(deserialize(s.substr(start,i-start)));
  53. start=i+1;
  54. }
  55. else if(s[i]=='[')
  56. {
  57. ++cnt;
  58. }
  59. else if(s[i]==']')
  60. {
  61. --cnt;
  62. }
  63. }
  64. return res;
  65. }
  66. };

参考:https://www.cnblogs.com/grandyang/p/5771434.html

385 Mini Parser 迷你解析器的更多相关文章

  1. [LeetCode] Mini Parser 迷你解析器

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

  2. 385. Mini Parser - LeetCode

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

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

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  4. 385. Mini Parser

    括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...

  5. 有时间了解一下Spark SQL parser的解析器架构

    1:了解大体架构 2:了解流程以及各个类的职责 3:尝试编写一个

  6. 第6章 网页解析器和BeautifulSoup第三方插件

    第一节 网页解析器简介作用:从网页中提取有价值数据的工具python有哪几种网页解析器?其实就是解析HTML页面正则表达式:模糊匹配结构化解析-DOM树:html.parserBeautiful So ...

  7. python 之网页解析器

    一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...

  8. EasyUI基础入门之Parser(解析器)

    前言 JQuery EasyUI提供的组件包含功能强大的DataGrid,TreeGrid.面板.下拉组合等.用户能够组合使用这些组件,也能够单独使用当中一个.(使用的形式是以插件的方式提供的) Ea ...

  9. FFmpeg的HEVC解码器源码简单分析:解析器(Parser)部分

    ===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...

随机推荐

  1. Ubuntu 16.04错误:正在读取软件包列表... 有错误! E: Encountered a section with no Package: header E: Problem with MergeList /var/lib/apt/lists/ppa.launchpad.net_t-tujikawa_ppa_ubuntu_dists_xenial_main_i18n_Translatio

    错误: 正在读取软件包列表... 有错误! E: Encountered a section with no Package: header E: Problem with MergeList /va ...

  2. Django学习系列之重写User模型和登录验证

    重写User模型 Django内置的User模型可能不适合某些项目,我们可能要基于内置的添加一些字段 创建users app startapp users 修改settings.py配置文件,覆盖默认 ...

  3. chassis & power

    机箱电源 ★ Main board ★ Voltage, connector ★ Hole ★ Ports ★ AT:12``*13.8`` or 12``*13``  30.5cm*33cm ★ B ...

  4. Cookie对象的特点

    1.存储少量不重要的数据2.存储在客户端的文本文件中(必须设置有效期,否则不被存储)3.安全性差4.存储的数据类型--字符串5.浏览器窗口无关,但与访问的站点相关6.具体特定的过期时间和日期7.在客户 ...

  5. 手动加入SSH支持、使用c3p0

    之前做的笔记,如今整理一下.大家有耐心的跟着做就能成功: SSH(struts2.spring.hibernate) *  struts2      *  充当mvc的角色 *  hibernate ...

  6. 扩展GCD 中国剩余定理(CRT) 乘法逆元模版

    extend_gcd: 已知 a,b (a>=0,b>=0) 求一组解 (x,y) 使得 (x,y)满足 gcd(a,b) = ax+by 以下代码中d = gcd(a,b).顺便求出gc ...

  7. nodejs参考文章

    http://www.cnblogs.com/lily1010/p/6683987.html https://manlili.github.io/2015/04/06/Node%E5%85%A5%E9 ...

  8. 【LeetCode刷题Java版】Reverse Words in a String

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  9. convex hull

    1 什么是convex hull 就是凸包,是计算几何中的一个概念,计算几何是计算机图形学的基础之一. 对于二维平面来说是这样的:对于二维平面上的点集,凸包是位于最外层的点构成的包围其它所有的点的凸多 ...

  10. presentModalViewController和dismissModalViewControllerAnimated的使用总结

    在实际开发中,如果要弹出视图: 我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法. presentModal ...