385. Mini Parser - LeetCode
Question
Solution
分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]
过程如下:
# 首先将字符串转化为字符数组,遍历每个字符
[ 压栈操作 NI(0, null)
123 给栈顶元素设置值 NI(0, NI(123))
, 不处理
[ 压栈操作 NI(0, NI(123)) | NI(0, null)
456 给栈顶元素设置值 NI(0, NI(123)) | NI(0, 456)
, 不处理
[ 压栈操作 NI(0, NI(123)) | NI(0, NI(456)) | NI(0, null)
789 给栈顶元素设置值 NI(0, NI(123)) | NI(0, NI(456)) | NI(0, NI(789))
] 出栈并将出栈后的元素添加到栈顶元素NI(0, NI(123)) | NI(0, [NI(456),NI(0, NI(789))])
] 出栈并将出栈后的元素添加到栈顶元素NI(0, [NI(123), NI(0, [NI(456), NI(0, NI(789))])])
] 不做处理
# 栈中最后一个元素就是返回结果
Java实现:
public NestedInteger deserialize(String s) {
if (!s.startsWith("[")) return new NestedInteger(Integer.parseInt(s));
Stack<NestedInteger> stack = new Stack<>();
// 123,[456,[789]]
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
char ch = arr[i];
if (ch == '[') {
stack.push(new NestedInteger());
} else if (ch == ']' & stack.size() > 1) {
NestedInteger pop = stack.pop();
stack.peek().add(pop);
} else if (Character.isDigit(ch) || ch == '-') {
boolean pos = true;
if (ch == '-') {
pos = false;
i++;
}
int num = 0;
while (i < arr.length && Character.isDigit(arr[i])) {
num *= 10;
num += arr[i++] - '0';
}
i--;
stack.peek().add(new NestedInteger(pos ? num : -num));
}
}
return stack.pop();
}
Reference
385. Mini Parser - LeetCode的更多相关文章
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 385 Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...
- 385. Mini Parser
括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...
- [LeetCode] Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- Leetcode: Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- LeetCode 385. Mini Parse
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- [Swift]LeetCode385. 迷你语法分析器 | Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- Pandas查询数据的几种方法
Pandas查询数据 Pandas查询数据的几种方法 df.loc方法,根据行.列的标签值查询 df.iloc方法,根据行.列的数字位置查询 df.where方法 df.query方法 .loc既能查 ...
- 【动态系统的建模与分析】9_一阶系统的频率响应_低通滤波器_Matlab/Simulink分析
magnitude response:振幅响应 phase response:相位响应 传递函数G(S)为什么将S看成jw化成G(jw)通过[动态系统的建模与分析]8_频率响应_详细数学推导 G(jw ...
- 前端面试题整理——关于EventLoop(1)
下面代码输出打印值顺序: async function async1(){ console.log('async1 start'); await async2(); console.log('asyn ...
- linux系统引导过程
linux系统引导过程 linux-0.11引导时,将依次运行BIOS程序.bootsect.s.setup.s和head.s,完成引导过程后进入到main函数运行.BIOS完成硬件的检查与初始化等工 ...
- C#编写一个简易的文件管理器
编写一个简易的文件管理器,通过本次实验,练习 TreeView.ListView 和SplitContainer 控件的使用,同时熟悉 C#文件系统的操作方法以及 File 类和 Directory类 ...
- Reflect.has检测对象是否拥有某个属性
Reflect.has({x: 0}, 'x'); // true Reflect.has({x: 0}, 'y'); // false // returns true for properties ...
- 使用基于Roslyn的编译时AOP框架来解决.NET项目的代码复用问题
理想的代码优化方式 团队日常协作中,自然而然的会出现很多重复代码,根据这些代码的种类,之前可能会以以下方式处理 方式 描述 应用时可能产生的问题 硬编码 多数新手,或逐渐腐坏的项目会这么干,会直接复制 ...
- 服务器的cpu 核心、线程
此版本有大范围改动,因为cpu作为一个大脑,所以更细致的进行了,相关的分析和阐述. 1.版本1. 2022.1.242.版本2: 2022.3.2 采集数据: ht2机器为物理机,cpu是4颗cpu, ...
- UML中类关系表示与Java代码中的对应关系
UML中类关系表示与Java代码中的对应关系 1. 类的UML表示法 上图中,Employee 类有两个String类型的私有属性和一个返回值为String类型public 方法 getName(); ...
- xpath 获取meta里的keywords及description的方法
html中的head的meta元素中的keywords与description标签如下: <meta name="keywords" content="xxxx&q ...