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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- C语言面试问答(3)
12个滑稽的C语言面试问答——<12个有趣的C语言问答>评析(3) 前文链接:http://www.cnblogs.com/pmer/p/3322429.html 5,atexit wit ...
- sql 清除日志空间
USE DBCenter GO SELECT file_id, name FROM sys.database_files; 查找日志名称 USE DBCenter ; GO ALTER DATABAS ...
- Bundles
Bundles 接着在Global.asax文件的Application_Start方法中调用BundleConfig.RegisterBundles方法: protected void Applic ...
- ios7上隐藏status bar
在iOS7上 对于设置status bar 又有了点点的改变 1.对于 UIViewController 加入了动态改变 status bar style的方法 - (UIStatusBarStyle ...
- 跟我一起玩转Sencha Touch 移动 WebApp 开发1
跟我一起玩转Sencha Touch 移动 WebApp 开发(一) 1.目录 移动框架简介,为什么选择Sencha Touch? 环境搭建 创建项目框架,框架文件简介 创建简单Tabpanel案例 ...
- Hibernate配置XML连接数据库
一.hibernate.cfg.xml 1.配置连接Oracle <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate ...
- Query插件
推荐一些常用感觉不错的jQuery插件 JQuery插件繁多,下面是个人在工作和学习中用到感觉不错的,特此记录. UI: jquery UI(官方的UI插件,很好很强大功能完备,灵活性很强,有十几套主 ...
- 什么时候用spring
论公司spring的滥用 这个公司每个项目用不同的一套开发框架,实在忍不住拿一个出来说说事.
- iOS核心应用对象
IOS应用之设计模式:模型-视图-控制器 iOS应用与其它应用的区别就在于它所管理的数据(和相应的业务逻辑)以及将数据展现给用户的方式.大多数UIKit对象并不定义应用而是帮助完善其行为.例如,你的应 ...
- 采用SOLR进行全文索引的完整解决方案,设计图