385. 迷你语法分析器

给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。

列表中的每个元素只可能是整数或整数嵌套列表

提示:你可以假定这些字符串都是格式良好的:

字符串非空
字符串不包含空格
字符串只包含数字0-9, [, - , ]

示例 1:

给定 s = "324",你应该返回一个 NestedInteger 对象,其中只包含整数值 324。

示例 2:

给定 s = "[123,[456,[789]]]",返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表:i.  一个 integer 包含值 456ii. 一个包含一个元素的嵌套列表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 迷你语法分析器相关推荐

  1. LeetCode 385. 迷你语法分析器(栈)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器. 列表中的每个元素只可能是整数或整数嵌套列表 提示:你可以假定这些字符串都是格式良好的: ...

  2. leetcode 385. 迷你语法分析器

    题目链接 思路:递归 分析:这个题主要在于要看懂题目意思,这个NestedInteger对象里面就是套了一个NestedInteger对象. 很明显,这是个嵌套的定义,那么我们可以用递归的思路来实现. ...

  3. 385. 迷你语法分析器

    给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器. 列表中的每个元素只可能是整数或整数嵌套列表 提示:你可以假定这些字符串都是格式良好的: 字符串非空 字符串不包含空格 字符串只包含数 ...

  4. leetcode 385. Mini Parser | 385. 迷你语法分析器(Java)

    题目 https://leetcode.com/problems/mini-parser/ 题解 只要有耐心分析清楚每一种状态就可以啦. /*** // This is the interface t ...

  5. LR(1)语法分析器生成器(生成Action表和Goto表)java实现(二)

    updata : 附我之前bilibili讲解视频链接 : https://www.bilibili.com/video/av63666423?share_medium=android&sha ...

  6. java实现语法分析器_200 行 JS 代码,带你实现代码编译器

    一.前言 对于前端同学来说,编译器可能适合神奇的魔盒 ,表面普通,但常常给我们惊喜. 编译器,顾名思义,用来编译,编译什么呢?当然是编译代码咯 . 其实我们也经常接触到编译器的使用场景: React ...

  7. 编译原理实验:Java实现SLR(1)语法分析器

    首先如果有熟悉Python的童鞋也可以看一看Python实现SLR(1)语法分析器,编译原理yyds!__FF_Y的博客-CSDN博客_python语法分析器,是我一个特别好的学长写的,这波属于传承了 ...

  8. 语法分析器 Java版

    想了解更多内容,移步至编译原理专栏 ==========================2021.12.22 更新=================================== 整理了一下代码 ...

  9. 基于Java语言的语法分析器

    LR(0)语法分析器 实验目的: 根据LR(0)语法分析的原理,编写.调试一个语法分析代码程序,对输入的句子进行分析. 实验工具: 使用了Java语言进行编写 ,使用Java开发工具IntelliJ ...

最新文章

  1. VMware下centos7安装VMware Tools
  2. 3.2.3 如何解决数据不匹配问题
  3. 基于MATLAB的面对对象编程(3)——事件和响应
  4. python位置参数ppt_如何在Python中使用一个或多个相同的位置参数?
  5. [转载] Python学习:Python成员运算符和身份运算符
  6. Python数模笔记-StatsModels 统计回归(1)简介
  7. 渐进式app_如何编写渐进式Web App新闻网站
  8. 微信公众号盈利模式_微信公众号的盈利模式有哪些?四种模式分享
  9. 判断字符串和数组是否为空
  10. 与AI人才有关的讨论
  11. java foreach顺序_Java中的LongStream forEachOrdered()方法
  12. w3c 菜鸟mysql_W3C 教程 | 菜鸟教程
  13. ASS字幕特效代码个人收藏
  14. 如何使用ListView分页
  15. NiFi ListSFTP精讲
  16. Coreldraw修改平面图独到漂亮见解就在这里了
  17. VOT竞赛paper阅读笔记
  18. vue开发微信公众号调用相机和相册(上传到自己的服务器)
  19. 计算机考试感受作文,考试后的感受作文(通用10篇)
  20. 7-1 电话聊天狂人 (25分) PTA 数据结构

热门文章

  1. 热传递物理模型matlab,温和灸热传递数学物理模型的分析与建立
  2. 第七周项目29-数组求员工工资
  3. 2021-2027全球与中国电动厕纸分配器市场现状及未来发展趋势
  4. 加速器:Expect脚本
  5. FPGA的spi flash配置-配置模式理解
  6. 曹健老师 TensorFlow2.1 —— 第二章 神经网络优化
  7. 有限长序列的z变换收敛域_求出以下序列的Z变换及收敛域
  8. 零基础CSS入门教程(13)–边框样式
  9. 什么是CVV2、CVC2、CVN2、CSC2码
  10. ADS-B机载全向天线 AV-74