385 Mini Parser 迷你解析器
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++:
- /**
- * // This is the interface that allows for creating nested lists.
- * // You should not implement it, or speculate about its implementation
- * class NestedInteger {
- * public:
- * // Constructor initializes an empty nested list.
- * NestedInteger();
- *
- * // Constructor initializes a single integer.
- * NestedInteger(int value);
- *
- * // Return true if this NestedInteger holds a single integer, rather than a nested list.
- * bool isInteger() const;
- *
- * // Return the single integer that this NestedInteger holds, if it holds a single integer
- * // The result is undefined if this NestedInteger holds a nested list
- * int getInteger() const;
- *
- * // Set this NestedInteger to hold a single integer.
- * void setInteger(int value);
- *
- * // Set this NestedInteger to hold a nested list and adds a nested integer to it.
- * void add(const NestedInteger &ni);
- *
- * // Return the nested list that this NestedInteger holds, if it holds a nested list
- * // The result is undefined if this NestedInteger holds a single integer
- * const vector<NestedInteger> &getList() const;
- * };
- */
- class Solution {
- public:
- NestedInteger deserialize(string s) {
- if(s.empty())
- {
- return NestedInteger();
- }
- if(s[0]!='[')
- {
- return NestedInteger(stoi(s));
- }
- if(s.size()<=2)
- {
- return NestedInteger();
- }
- NestedInteger res;
- int cnt=0;
- int start=1;
- for(int i=1;i<s.size();++i)
- {
- if(cnt==0&&s[i]==','||i==s.size()-1)
- {
- res.add(deserialize(s.substr(start,i-start)));
- start=i+1;
- }
- else if(s[i]=='[')
- {
- ++cnt;
- }
- else if(s[i]==']')
- {
- --cnt;
- }
- }
- return res;
- }
- };
参考:https://www.cnblogs.com/grandyang/p/5771434.html
385 Mini Parser 迷你解析器的更多相关文章
- [LeetCode] Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- 385. Mini Parser - LeetCode
Question 385. Mini Parser Solution 分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]过程如下: # ...
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 385. Mini Parser
括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...
- 有时间了解一下Spark SQL parser的解析器架构
1:了解大体架构 2:了解流程以及各个类的职责 3:尝试编写一个
- 第6章 网页解析器和BeautifulSoup第三方插件
第一节 网页解析器简介作用:从网页中提取有价值数据的工具python有哪几种网页解析器?其实就是解析HTML页面正则表达式:模糊匹配结构化解析-DOM树:html.parserBeautiful So ...
- python 之网页解析器
一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...
- EasyUI基础入门之Parser(解析器)
前言 JQuery EasyUI提供的组件包含功能强大的DataGrid,TreeGrid.面板.下拉组合等.用户能够组合使用这些组件,也能够单独使用当中一个.(使用的形式是以插件的方式提供的) Ea ...
- FFmpeg的HEVC解码器源码简单分析:解析器(Parser)部分
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
随机推荐
- 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 ...
- Django学习系列之重写User模型和登录验证
重写User模型 Django内置的User模型可能不适合某些项目,我们可能要基于内置的添加一些字段 创建users app startapp users 修改settings.py配置文件,覆盖默认 ...
- chassis & power
机箱电源 ★ Main board ★ Voltage, connector ★ Hole ★ Ports ★ AT:12``*13.8`` or 12``*13`` 30.5cm*33cm ★ B ...
- Cookie对象的特点
1.存储少量不重要的数据2.存储在客户端的文本文件中(必须设置有效期,否则不被存储)3.安全性差4.存储的数据类型--字符串5.浏览器窗口无关,但与访问的站点相关6.具体特定的过期时间和日期7.在客户 ...
- 手动加入SSH支持、使用c3p0
之前做的笔记,如今整理一下.大家有耐心的跟着做就能成功: SSH(struts2.spring.hibernate) * struts2 * 充当mvc的角色 * hibernate ...
- 扩展GCD 中国剩余定理(CRT) 乘法逆元模版
extend_gcd: 已知 a,b (a>=0,b>=0) 求一组解 (x,y) 使得 (x,y)满足 gcd(a,b) = ax+by 以下代码中d = gcd(a,b).顺便求出gc ...
- nodejs参考文章
http://www.cnblogs.com/lily1010/p/6683987.html https://manlili.github.io/2015/04/06/Node%E5%85%A5%E9 ...
- 【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 ...
- convex hull
1 什么是convex hull 就是凸包,是计算几何中的一个概念,计算几何是计算机图形学的基础之一. 对于二维平面来说是这样的:对于二维平面上的点集,凸包是位于最外层的点构成的包围其它所有的点的凸多 ...
- presentModalViewController和dismissModalViewControllerAnimated的使用总结
在实际开发中,如果要弹出视图: 我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法. presentModal ...