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. c的free 为什么不需要知道大小

    malloc malloc函数在运行时分配内存.它需要以字节为单位的大小并在内存中分配那么多空间.这意味着malloc(50)将在内存中分配50个字节.它返回一个void指针 calloc 与mall ...

  2. Linux编程 | 使用 make

    目录 简单的 makefile 文件 常规的 makefile 文件 常用参数 make 内置规则 后缀和模式规则 make 管理函数库 在Linux 环境中,make 是一个非常重要的编译命令.不管 ...

  3. HTML中meta标签详解;property=og标签详解

    meta是用来在HTML文档中模拟HTTP协议的响应头报文.META标签是HTML语言HEAD区的一个辅助性标签,它位于HTML文档头部的<HEAD>标记和<TITLE>标记之 ...

  4. WIN进程注入&BypassUAC&令牌窃取

    WIN进程注入&BypassUAC&令牌窃取 本地提权-win令牌窃取 假冒令牌可以假冒一个网络中的另一个用户进行各类操作. 所以当一个攻击者需要域管理员的操作权限时候,需通过假冒域管 ...

  5. Mybatis多表查询出现null字段

    写在前面 今天使用mybatis实现多表查询,记录一下其中遇到的坑 mybatis多表查询简介 mybatis多表查询主要有两个方式,通俗易懂的来说就是一个是查询少量属性(association),一 ...

  6. 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?

    前言 OK,我也来造轮子了 博客系统从一开始用WordPress,再到后来用hexo.hugo之类的静态博客生成放github托管,一直在折腾 折腾是为了更好解决问题,最终还是打算自己花时间搞一个好了 ...

  7. LC-707

    设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...

  8. Intel主板芯片组

    写这个的初衷还是由于linux内核本身就是硬件的抽象,如果你对硬件的相关发展,机制以及架构不了解,实际你也是看不懂linux内核代码以及看不懂linux很多命令输出的结果的,如果你看内核代码就会发现内 ...

  9. Model, HttpServletRequest, ModelMap区别

    看了spring mvc的底层会发现,model数据最终还是写到HttpServletRequest属性中,只是model的写法更体现了MVC思想减少各层间耦合 写法: 1.request.setAt ...

  10. Win下GCC报错 error: ‘for’ loop initial declarations are only allowed in C99 mode

    ##报错## 用GCC编译for循环会出现以下错误 error: 'for' loop initial declarations are only allowed in C99 mode 如图所示: ...