Java实现 LeetCode 385 迷你语法分析器
385. 迷你语法分析器
给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。
列表中的每个元素只可能是整数或整数嵌套列表
提示:你可以假定这些字符串都是格式良好的:
字符串非空
字符串不包含空格
字符串只包含数字0-9, [, - , ]
示例 1:
给定 s = "324",
你应该返回一个 NestedInteger 对象,其中只包含整数值 324。
示例 2:
给定 s = "[123,[456,[789]]]",
返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表:
i. 一个 integer 包含值 456
ii. 一个包含一个元素的嵌套列表
a. 一个 integer 包含值 789
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
public NestedInteger deserialize(String s) {
if(s.charAt(0)!='[') {
return new NestedInteger(Integer.valueOf(s));
}
else {
return deserialize1(s.substring(1));
}
}
public NestedInteger deserialize1(String s) {
NestedInteger res = new NestedInteger();
//从左到右扫描
for(int i=0;i<s.length();i++) {
char c = s.charAt(i);
if(c>='0'&&c<='9'||c=='-') {
int n = 0; int flag = 1;
for(;i<s.length();i++) {
c = s.charAt(i);
if(c>='0'&&c<='9') {
n = n*10 + c-'0';
} else if(c=='-'){
flag = -1;
} else {
i = i-1;
break;
}
}
res.add(new NestedInteger(flag*n));
}
else if(c=='[') {
int index = i;
int counter = 0;
for(;i<s.length();i++) {
c = s.charAt(i);
if(c=='[') counter++;
else if(c==']') counter--;
if(counter==0) {
res.add(deserialize1(s.substring(index+1,i)));
break;
}
}
}
}
return res;
}
}
Java实现 LeetCode 385 迷你语法分析器的更多相关文章
- [Swift]LeetCode385. 迷你语法分析器 | Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- Java实现 LeetCode 736 Lisp 语法解析(递归)
736. Lisp 语法解析 给定一个类似 Lisp 语句的表达式 expression,求出其计算结果. 表达式语法如下所示: 表达式可以为整数,let 语法,add 语法,mult 语法,或赋值的 ...
- LR(1)语法分析器生成器(生成Action表和Goto表)java实现(二)
本来这次想好好写一下博客的...结果耐心有限,又想着烂尾总比断更好些.于是还是把后续代码贴上.不过后续代码是继续贴在BNF容器里面的...可能会显得有些臃肿.但目前管不了那么多了.先贴上来吧hhh.说 ...
- LR(1)语法分析器生成器(生成Action表和Goto表)java实现(一)
序言 : 在看过<自己实现编译器链接器>源码之后,最近在看<编译器设计>,但感觉伪代码还是有点太浮空.没有掌握的感觉,也因为内网几乎没有LR(1)语法分析器生成器的内容,于是我 ...
- 开源语法分析器--ANTLR
序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...
- org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 语法分析器在此文档中遇到多个 "64,000" 实体扩展; 这是应用程序施加的限制
使用SAX解析XML文件.XML文件有1.5G,程序抛出了这个问题: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 语法 ...
- 03.从0实现一个JVM语言系列之语法分析器-Parser-03月01日更新
从0实现JVM语言之语法分析器-Parser 相较于之前有较大更新, 老朋友们可以复盘或者针对bug留言, 我会看到之后答复您! 源码github仓库, 如果这个系列文章对你有帮助, 希望获得你的一个 ...
- 编译原理简单语法分析器(first,follow,分析表)源码下载
编译原理(简单语法分析器下载) http://files.cnblogs.com/files/hujunzheng/%E5%8A%A0%E5%85%A5%E5%90%8C%E6%AD%A5%E7%AC ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- MATLAB1127(传递函数)
sys=tf(400,[1,50,0]) sys = 400 ---------- s^2 + 50 s 其中,tf()函数的用法. 传递函数 dsys=c2d(sys,ts,'z') dsys ...
- Mysql常用sql语句(21)- regexp 正则表达式查询
测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 正则的强大不言而喻,Mysql中也提供了 reg ...
- An SWT error has occurred
对话框标题:Problem Occurred 对话框内容:Unhandled event loop exception No more handles 对话框按钮:第一个超链接:Show Error ...
- Python --函数学习2
一.函数参数和返回值 --参数:负责给函数传递一些必要的数据或者信息 -形参(形式参数):在函数定义的时候用到的参数,没有具体值,只是一个占位符号 -实参(实际参数):在调用函数的时候输入的值 exa ...
- ES6新增API
1.Object.assign(a,b,c) a.b均为对象,意思是把b对象的属性添加到a上面去.如果a中已经定义了某个属性,b也定义了的话就会覆盖a的,就是后面覆盖前面的,后面生命的有效.是 一种浅 ...
- Unity3D中UGUI不使用DOTween制作渐隐渐现效果
在做UI后期设计时,我们可能要对UI做一些特效,这篇文章我们来学习下如何在Unity3d中对实现渐隐渐现的效果, 首先我们看下Unity New UI即UGUI中渐隐渐现的做法. 观察我们会发现Uni ...
- HTML5新特性-- -定时器
一.定时器:一次性定时器/周期性定时器 #requestAnimationFrame 智能定时器 #此定时器主要使用范围:动画和游戏中 特点: setTimeout(fn,500); setInter ...
- MySQL慢查询优化(线上案例调优)
文章说明 这篇文章主要是记录自己最近在真实工作中遇到的慢查询的案例,然后进行调优分析的过程,欢迎大家一起讨论调优经验.(以下出现的表名,列名都是化名,实际数据也进行过一点微调.) PS:最近做了一个面 ...
- mysql小白系列_08 zabbix3.2.6概念及部署
一 zabbix功能简介 1.zabbix三大监控组件 zabbix server web gui database zabbix_server zabbix proxy agent client 2 ...
- SimpleAuthenticationInfo的参数
SimpleAuthenticationInfo的参数 仅供个人参考,以及学习记录.SimpleAuthenticationInfo authenticationInfo = new SimpleAu ...