Question

385. Mini Parser

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的更多相关文章

  1. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  2. 385 Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...

  3. 385. Mini Parser

    括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...

  4. [LeetCode] Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  5. Leetcode: Mini Parser

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  6. LeetCode 385. Mini Parse

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  7. [Swift]LeetCode385. 迷你语法分析器 | Mini Parser

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

随机推荐

  1. window onerror 各浏览器下表现总结

    window onerror 各浏览器下表现总结 做前端错误上报,必然离不开window onerror,但window onerror在不同设备上表现并不一致,浏览器为避免信息泄露,在一些情况下并不 ...

  2. This program may be freely redistributed under the terms of the GNU GPL

    在centos中安装Google浏览器时 执行[root@server1 opt]# rpm ivh install google-chrome-stable_current_x86_64.rpm 爆 ...

  3. AcWing 1222. 密码脱落

    题目链接 题目描述: X星球的考古学家发现了一批古代留下来的密码. 这些密码是由A.B.C.D 四种植物的种子串成的序列. 仔细分析发现,这些密码串当初应该是前后对称的(也就是我们说的镜像串). 由于 ...

  4. win11拖动窗口造成崩溃的问题

    问题描述 拖动窗口,随机概率出现 屏幕闪烁 屏幕黑屏 屏幕瞬间分屏 解决方法 windowes11贴吧大神给的方案 1,按下 win键+R 输入 regedit 进入注册表,进入以下路径:计算机\HK ...

  5. 帝国CMS 后台登录空白

    编辑/e/config/config.php中 $ecms_config['esafe']['ckfromurl']=0; //是否启用来源地址验证,0为不验证,1为全部验证,2为后台验证,3为前台验 ...

  6. 利用js获取不同页面间跳转需要传递的参数

    获取参数的js函数如下: function GetQueryValue(queryName) { var query = decodeURI(window.location.search.substr ...

  7. Java之JDBC详谈(数据库)

    详细介绍了数据库的JDBC操作,并且整理了具体方法,有代码实现与详细注释.

  8. 微信小程序wx.login先执行onLaunch与onLoad加载顺序问题

    @ 目录 遇到问题 请求api返回需要先登录,实际上登录已成功 问题分析 解决问题 自定义回调函数 app.js index.js 扩展提问 学习交流 随机数字随机幸运数+ My Blog 技术交流 ...

  9. MySql免安装版 Error 2003 Can connect to MySQL server on ...

    现象描述:mysql只能本地登录,无法远程登录 解决方案: 1. 查看mysql端口(默认端口3306,命令端口根据需要修改),发现只有本地连接端口开放. netstat -an|findstr 33 ...

  10. 企业级 Web 开发的挑战

    本文翻译自土牛Halil ibrahim Kalkan的<Mastering ABP Framework>,是系列翻译的起头,适合ABP开发人员或者想对ABP框架进行深入演进的准架构师. ...