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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- linux挂载分区
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda2 36G .3G 32G % / tmpf ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC
使用 NPC,NPCManager 在 XNA 中创建 NPC 平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐 ...
- 关于Java、Python、Go编程思想的不同
Go学习笔记 - 关于Java.Python.Go编程思想的不同 看了两周七牛团队翻译的<Go语言程序设计>,基本上领略到了Go语言的魅力.学习一个语言,语法什么的任何人都是很容易学会,难 ...
- 不想作死系列--win7远程linux桌面之vncserver
1.在linux服务器上安装vncserver yum install vncserver 或者下载相应linux版本的tigervnc-serverrpm rpm -ivh tigervnc-ser ...
- Android: Type Method 'NewStringUTF' could not be resolved
编译一个最简单的Android NDK 例子(android-ndk-r8/samples/hello-jni)出现了错误信息:Type Method 'NewStringUTF' could n ...
- 本地数据jqGrid分页
var mydata=''; $(function() { var str = ''; str += "<span>共<span id='p_total'></ ...
- 给你的git仓库瘦身
很久没有写博客了,最近遇到了一个git问题,比较典型,记录下来与大家分享. 我们使用git版本控制的时候享受了很多便利,不管是代码合并,分支提供给我们的并发,但我们也往往忽略了每次提交之后在我们本地项 ...
- html - table 表格不被撑开,td某些列宽度固定某些列自适应
table-layout 属性的解释:http://www.w3school.com.cn/cssref/pr_tab_table-layout.asp 1,实现table细边框,设置如下css: t ...
- [资源]网上常用免费WebServices集合 转载
引用地址:http://www.cocoachina.com/bbs/simple/?t54338_5.html 天气预报Web服务,数据来源于中国气象局 公用事业 http://www.webxml ...
- 【JS学习笔记】提取行间事件
行间提取事件第一种方法: function 名字() { ... } oBtn.onclick=名字: 第二种方法: oBtn.onclick=function () { ... } 其实在JS当中, ...