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总的的边数减去重叠的 ...
随机推荐
- Dockerize an ASP.NET Core application
原文:Dockerize an ASP.NET Core application 介绍 本示例演示了如何对ASP.NET Core应用程序进行容器化. 为什么要构建ASP.NET Core? 开源 在 ...
- 解决 css 浮动后 父元素高度失效问题
应用场景 子元素标签使用 浮动后,会出现浮在父元素上层,脱离了.导致父元素没办法根据子元素的高度而变化,提供以下解决方案. 解决代码 把 '.clearfix ' Class 样式添加到 父元素即可. ...
- 7步学会在Windows下上架iOS APP流程
之前用跨平台开发工具做了一个应用,平台可以同时生成安卓版和苹果版,想着也把这应用上架到App Store试试,于是找同学借了个苹果开发者账号,但没那么简单,还要用到Mac电脑的钥匙串申请发布证书和上传 ...
- C++中的算法头文件<algorithm>,<numeric>和<functional>
算法部分主要由头文件<algorithm>,<numeric>和<functional>组成.<algorithm>是所有STL头文件中最大的一个,它是 ...
- Java 实例 - 读取文件内容
原文作者:菜鸟教程 原文链接:Java 实例 - 读取文件内容(建议前往原文以获得最佳体验) 按行读取文本文件 import java.io.*; public class Main { public ...
- Istio实践(1)-环境搭建及应用部署
1. Istio简介 Istio是最初由IBM,Google和Lyft开发的服务网格的开源实现.它可以透明地分层到分布式应用程序上,并提供服务网格的所有优点,例如流量管理,安全性和可观察性. 它旨在与 ...
- Java基础语法01——变量与运算符
本文是对Java基础语法的第一部分的学习,包括注释:标识符的命名规则与规范:变量的数据类型分类以及转换:以及六种运算符(算术.赋值.比较.逻辑.三元和位运算符).
- java获取登录ip和地址
//获取HttpServletRequest对象 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestCon ...
- 查域名对应ip,测试端口是否可访问通
根据命令查询软件包名称 yum provides */nslookup 根据域名解析ip nslookup 域名 示例:nslookup smtp.163.com 测试端口 telnet ip 端口 ...
- Protoc安装
系统:linux 记住,千万别混乱版本,一般protoc可执行文件在/usr/local/bin/或/usr/bin/下 个人这里使用3.13版本示例,下面两部分命令可以写入shell脚本,记住执行要 ...