当前位置:网站首页>力扣解法汇总358-迷你语法分析器
力扣解法汇总358-迷你语法分析器
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:
描述:
给定一个字符串 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这种对称的结构最适合使用栈的思想来接题目。 * 由于本题当中,对象有两种形式,数组和数字形式。所以我们先生成对象加入到集合当中,后续遍历过程中再决定其类型。 * 栈的话用来装载一层一层的对象,读到[则说明增加增加一层级。读到]则介绍一层级。 * 针对字符从前向后遍历,每个字符分为以下几种情况: * 1.字符串为'['时,则说明当前对象是一个数组的形式,则生成一个对象加入到数组中(此时还未确定时数组还是数字形式)。 * 2.字符串为']'时,则说明当前数组的循环结束,则从栈中pop出最上层节点。 * 3.字符串为'.'时,则说明应该读取下一组对象。 * 4.字符串为数字时,则记录一下,start++。除此之外,除了数字类型,都会触发字符串结束判断,生成一个数字类型的对象加入到最上层的栈对象中。
代码:
public class Solution385 {
public NestedInteger deserialize(String s) {
Stack<NestedInteger> stack = new Stack<>();
NestedInteger root = new NestedInteger();
stack.add(root);
StringBuilder builder = new StringBuilder();
int start = 0;
char[] chars = s.toCharArray();
while (true) {
if (start == chars.length) {
addValue(builder, stack);
break;
}
char aChar = chars[start];
if (aChar == '[') {
NestedInteger value = new NestedInteger();
stack.peek().getList().add(value);
stack.add(value);
start++;
continue;
}
if (aChar == ']') {
addValue(builder, stack);
stack.pop();
start++;
continue;
}
if (aChar == ',') {
addValue(builder, stack);
start++;
continue;
}
builder.append(aChar);
start++;
}
NestedInteger result = stack.pop().getList().get(0);
return result;
}
private void addValue(StringBuilder builder, Stack<NestedInteger> stack) {
if (builder.length() == 0) {
return;
}
NestedInteger value = new NestedInteger(Integer.parseInt(builder.toString()));
stack.peek().add(value);
builder.setLength(0);
}
class NestedInteger {
Integer mValue;
List<NestedInteger> list = new ArrayList<>();
public NestedInteger() {
}
public NestedInteger(int value) {
this.mValue = value;
}
public boolean isInteger() {
return mValue != null;
}
public Integer getInteger() {
return mValue;
}
public void setInteger(Integer value) {
this.mValue = value;
this.list = null;
}
public void add(NestedInteger ni) {
list.add(ni);
}
public List<NestedInteger> getList() {
return list;
}
}
}边栏推荐
- Leetcode 45 jump game II
- virsh创建/关闭/停止虚拟机常用的几条指令
- How can low code platforms improve cost effectiveness?
- Advantages of Google ads
- Don't miss it! Five large data visualization screens that HR must collect
- MySQL advanced knowledge points
- 力扣解法汇总732-我的日程安排表 III
- 力扣解法汇总436-寻找右区间
- 力扣解法汇总668-乘法表中第k小的数
- 力扣解法汇总944-删列造序
猜你喜欢

入手Ticwatch2

MySQL表常用操作思维导图

leetcodeSQL:612. Nearest distance on plane

Three main factors determining advertising quality

Explore performance optimization! Performance improvement from 2 months to 4 hours!

ozzanimation-基于sse的动作系统

kali安装empire过程中遇到的各种报错解决方案

MySQL advanced knowledge points

Linux(CentOS6)安装MySQL5.5版本数据库

2022 blind box applet app has become a new drainage outlet for enterprises
随机推荐
通过搜索广告附加信息让广告更具相关性
The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
MySQL advanced knowledge points
The most comprehensive redis transaction control in 2022 (with illustration)
力扣解法汇总473-火柴拼正方形
Why do we use Google search ads?
PHP builds a high-performance API architecture based on sw-x framework (III)
力扣解法汇总面试题 01.05. 一次编辑
MySQL高级部分知识点
Google Ads 竞价的运作机制
Database
Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions
Graphic data analysis | business cognition and data exploration
广泛匹配修饰符符号已经被弃用,请勿使用
How WPS inserts a directory and the operating steps for quickly inserting a directory
力扣解法汇总450-删除二叉搜索树中的节点
Google 搜索广告系列设置前有哪些准备工作?
力扣解法汇总953-验证外星语词典
“中国东信杯”广西大学第四届程序设计竞赛(同步赛)