当前位置:网站首页>使用栈来解决”迷你语法分析器“的问题
使用栈来解决”迷你语法分析器“的问题
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
边栏推荐
- In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
- MySQL insert free column
- The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
- Liunx foundation - zabbix5 0 monitoring system installation and deployment
- Difference between relative path and absolute path (often asked in interview)
- 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
- Numpy stack function
- Centos7 install MySQL 8 0
- Classification of technology selection (2022)
- 《信息系统项目管理师总结》第四章 项目成本管理
猜你喜欢

微软是如何解决 PC 端程序多开问题的
![Introduction to ACM [inclusion exclusion theorem]](/img/3a/9bc2a972d7587aab51fceb8cd2b9bd.png)
Introduction to ACM [inclusion exclusion theorem]

Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)

Processes and threads

Service avalanche effect
![How to use C language to realize [guessing numbers game]](/img/8c/052dcb0ce64ee1713bebb1340248e6.png)
How to use C language to realize [guessing numbers game]

Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)

REINFORCE

Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮

Innovation and management based on Scrum
随机推荐
Chapter VII project communication management of information system project manager summary
对.NET未来的一点感悟
SQL statement - DDL
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Encapsulate components such as pull-down menu based on ele
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
ASP.NET 6 中间件系列 - 执行顺序
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
[learn junit5 from official documents] [II] [writingtests] [learning notes]
Openfeign service call
Winsock programming interface experiment: Ping
《信息系统项目管理师总结》第五章 项目质量管理
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Service avalanche effect
C# 11 的这个新特性,我愿称之最强!
Classification of technology selection (2022)
Chapter IV project cost management of information system project manager summary
AOT和单文件发布对程序性能的影响