当前位置:网站首页>使用栈来解决”迷你语法分析器“的问题
使用栈来解决”迷你语法分析器“的问题
2022-04-23 03:02:00 【&小小白&】
十三、 迷你语法分析器
13.1、题设要求
给定一个字符串 s 表示一个整数嵌套列表,实现一个解析它的语法分析器并返回解析的结果 NestedInteger 。
列表中的每个元素只可能是整数或整数嵌套列表
示例 1:
输入:s = "324",
输出:324
解释:你应该返回一个 NestedInteger 对象,其中只包含整数值 324。
示例 2:
输入:s = "[123,[456,[789]]]",
输出:[123,[456,[789]]]
解释:返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表:
i. 一个 integer 包含值 456
ii. 一个包含一个元素的嵌套列表
a. 一个 integer 包含值 789
提示:
1 <= s.length <= 5 * 104
s 由数字、方括号 "[]"、负号 '-' 、逗号 ','组成
用例保证 s 是可解析的 NestedInteger
输入中的所有值的范围是 [-106, 106]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/mini-parser
13.2、解题思路
从左至右遍历s,如果遇到 “[” ,则表示是一个新的NestedInteger实例,需要将其入栈.如果遇到 “]” 或 “,” ,则表示是一个数字或者NestedInteger实例的结束,需要将其添加入栈顶的NestedInteger实例。最后返回栈顶的实例即可。
13.3、算法
/** * // 这是允许创建嵌列表的接口 * // 你不应该实现它,也不应该猜测它的实现 * public interface NestedInteger { * // 构造函数初始化一个空的嵌套列表 * public NestedInteger(); * * // 构造函数初始化单个整数 * public NestedInteger(int value); * * // 如果此NestedInteger包含单个整数,而不是嵌套列表,则返回True * public boolean isInteger(); * * // 如果NestedInteger包含单个整数,则返回此NestedInteger保存的单个整数 * // 如果此NestedInteger包含嵌套列表,则返回NULL * public Integer getInteger(); * * // 将此NestedInteger设置为包含单个整数 * public void setInteger(int value); * * // 设置此NestedInteger以保存嵌套列表并向其添加嵌套整数 * public void add(NestedInteger ni); * * // 如果NestedInteger包含嵌套列表,则返回此NestedInteger保存的嵌套列表 * // 如果此NestedInteger包含单个整数,则返回空列表 * public List<NestedInteger> getList(); * } */
//parseInt()函数:用于解析一个字符串,并返回一个整数
//isDigit()函数:检查参数是否为十进制数字字符
class Solution {
//从左至右遍历s,如果遇到"[",则表示是一个新的NestedInteger实例,需要将其入栈.
//如果遇到"]"或",",则表示是一个数字或者NestedInteger实例的结束,需要将其添加入栈顶的NestedInteger实例.
//最后返回栈顶的实例即可.
public NestedInteger deserialize(String s) {
//没有"[",就一个整数,结束
if(s.charAt(0) != '['){
return new NestedInteger(Integer.parseInt(s));
}
//定义一个栈来存放数据
Deque<NestedInteger> stack = new ArrayDeque<>();
int num = 0;
boolean negative = false;
for (int i = 0; i < s.length(); i++) {
//将字符串中的字符一一取出
char c = s.charAt(i);
//字符为'-',输出true
if (c == '-') {
negative = true;
}else if (Character.isDigit(c)){
//如果字符为数字,压入栈中
num = num * 10 + c - '0';
}else if (c == '['){
//如果字符为'[',压入栈中
stack.push(new NestedInteger());
}else if (c == ',' || c == ']'){
//如果遇到']'或',',将数字或者NestedInteger实例添加入栈顶的NestedInteger实例
if (Character.isDigit(s.charAt(i-1))){
if (negative){
num *= -1;
}
stack.peek().add(new NestedInteger(num));
}
num = 0;
negative = false;
if (c == ']' && stack.size() > 1){
NestedInteger ni = stack.pop();
stack.peek().add(ni);
}
}
}
return stack.pop();
}
}
版权声明
本文为[&小小白&]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_52916408/article/details/124197762
边栏推荐
- Array and collection types passed by openfeign parameters
- BLDC double closed loop (speed PI + current PI) Simulink simulation model
- 基于ele封装下拉菜单等组件
- Specific field information of MySQL export table (detailed operation of Navicat client)
- 腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
- VirtualBox virtual machine (Oracle VM)
- Huawei machine test question -- deformation of hj53 Yang Hui triangle
- ASP.NET 6 中间件系列 - 自定义中间件类
- Restart redis
- Introduction to ACM [TSP problem]
猜你喜欢
ASP.NET 6 中间件系列 - 条件中间件
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
C# WPF UI框架MahApps切换主题
Depth deterministic strategy gradient (ddpg)
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
ASP.NET和ASP.NETCore多环境配置对比
Innovation and management based on Scrum
Array and collection types passed by openfeign parameters
FileNotFoundError: [Errno 2] No such file or directory
tf. keras. layers. MaxPooling? D function
随机推荐
tf. keras. layers. Embedding function
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
tf. keras. layers. MaxPooling? D function
Small companies don't make formal offers
Specific field information of MySQL export table (detailed operation of Navicat client)
《信息系統項目管理師總結》第六章 項目人力資源管理
tf. keras. layers. Timedistributed function
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
[ncnn] - the meaning of - 23300 in param
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
AspNetCore配置多环境log4net配置文件
《信息系统项目管理师总结》第四章 项目成本管理
The usage of case when and select case when is very easy to use
Openfeign details show
C# 11 对 ref 和 struct 的改进
Service avalanche effect
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)