LeetCode 385. Mini Parse
Given a nested list of integers represented as a string, implement a parser to deserialize it.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Note: You may assume that the string is well-formed:
- String is non-empty.
- String does not contain white spaces.
- String contains only digits
0-9,[,-,,].
Example 1:
Given s = "324", You should return a NestedInteger object which contains a single integer 324.
Example 2:
Given s = "[123,[456,[789]]]", Return a NestedInteger object containing a nested list with 2 elements: 1. An integer containing value 123.
2. A nested list containing two elements:
i. An integer containing value 456.
ii. A nested list with one element:
a. An integer containing value 789.
Subscribe to see which companies asked this question
【题目分析】
用String的形式给定一个嵌套的list,然后实现对这个字符串的反序列化。给定的字符串满足下列要求:
1. 非空
2. 不包含空格
3. 只包含0-9,[,],- 这几种字符
【思路】
对于嵌套的形式,我们要把一对中括号括起来的内容嵌套进中括号外面的内容中。
我们使用栈来保存之前的内容,当前一对中括号的内容如果构造完毕,就把它嵌套进栈中之前的一个元素。为此我们需要做以下几件事情:
1. 遍历字符串
2. 如果当前字符是'['则新建一个NestedInteger入栈
3. 如果当前字符是0-9或者-,则加入到当前数字字符串序列中
4. 如果当前字符是','或者']',如果数字字符串非空,则把该数字加入到栈顶的NestedInteger中,如果当前字符是']'则还要把栈顶元素弹出并添加到新的栈顶元素中
5. 遍历结束后如果最后的数字序列非空,则把该数字加入栈顶元素并返回栈顶元素,否则直接返回栈顶元素。
【java代码】
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class Solution {
public NestedInteger deserialize(String s) {
Stack<NestedInteger> sk = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if(ch == '[') {
sk.push(new NestedInteger());
}
else if((ch >= '0' && ch <= '9') || ch == '-') {
sb.append(ch);
}
else {
if(sb.length() >= 1) {
int num = Integer.parseInt(sb.toString());
NestedInteger ni = new NestedInteger(num);
NestedInteger top = sk.peek();
top.add(ni);
sb.delete(0, sb.length());
}
if(ch == ']') {
NestedInteger top = sk.pop();
if(sk.isEmpty()) return top;
NestedInteger newtop = sk.peek();
newtop.add(top);
}
}
}
if (sb.length() >= 1)
return new NestedInteger(Integer.parseInt(sb.toString()));
return sk.peek();
}
}
LeetCode 385. Mini Parse的更多相关文章
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 385. Mini Parser - LeetCode
Question 385. Mini Parser Solution 分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]过程如下: # ...
- 385 Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...
- Java实现 LeetCode 385 迷你语法分析器
385. 迷你语法分析器 给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器. 列表中的每个元素只可能是整数或整数嵌套列表 提示:你可以假定这些字符串都是格式良好的: 字符串非空 字符串 ...
- 385. Mini Parser
括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...
- Leetcode 385.字典序排序
字典序排序 给定一个整数 n, 返回从 1 到 n 的字典顺序. 例如, 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] . 请尽可能的优化算法的时间复杂度和 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Oracle PLSQL笔记(过程的创建和及调用)
过程(procedure): 用于在数据库中完成特定的操作或者任务.是一个PLSQL程序块,可以永久的保存在数据库中以供其他程序调用. 一.创建所需的表USERS create table users ...
- Python:Python学习总结
Python:Python学习总结 背景 PHP的$和->让人输入的手疼(PHP确实非常简洁和强大,适合WEB编程),Ruby的#.@.@@也好不到哪里(OO人员最该学习的一门语言). Pyth ...
- 一个意想不到的Javascript内存泄漏
原文:http://point.davidglasser.net/2013/06/27/surprising-javascript-memory-leak.html 本周我在Meter的同事追踪到了一 ...
- [修]python普通继承方式和super继承方式
[转]python普通继承方式和super继承方式 原文出自:http://www.360doc.com/content/13/0306/15/9934052_269664772.shtml 原文的错 ...
- LINUX安装SVN+添加自动同步+远程下载最新代码
LINUX安装SVN+添加自动同步+远程下载最新代码---------------------1. 新建一个用户:svnroot ,以下操作非特别说明皆为root用户操作--------------- ...
- java动态加载配置文件
最近项目中需要做定时任务,即定时数据库的备份.定时时间用户可以在界面中配置,要求配置修改好立即生效. 想不到什么好办法.下面是一种实现思路 把用户配置的时间存到properties配置文件中,定时任务 ...
- Bitmap的一个简单实现
一.Bitmap简介 Bitmap是一种常用的数据结构,其实就是一个连续的数组,主要是用于映射关系,如映射整数,一位代表一个数,即这里假设Bitmap有100Bytes * 8 这么多的位,那么这里可 ...
- 用vue实现简单实时汇率计算功能
最近在自己摸索vue的使用,因为相对于只是去看教程和实例,感觉不如自己动手写一个demo入门来的快.刚好看到小程序中有一个简单但是很精致的应用极简汇率,而且它的表现形式和vue的表现形式很像,于是想着 ...
- 怎样求FIRST集、FOLLOW集和SELECT集
一,要知道什么是终结符和非终结符. 终结符:通俗的说就是不能单独出现在推导式左边的符号,也就是说终结符不能再进行推导. 非终结符:不是终结符的都是非终结符.(非男即女,呵呵) 如:A-->B,则 ...
- 驱动07.USB驱动程序
1 了解USB识别的过程 eg:在Windows系统下的一个现象:把手机的USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱 ...