385. Mini Parser
括号题一般都是stack..
一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力。
后来猛然发现其实可以直接吧NestedInteger作为Object放入Stack里。
这种直接往堆顶元素里放的办法一定要注意。
然后就是edge cases多得一逼,一定要仔细,看了一刷的答案做的,有点后悔。其实有时候觉得麻烦的时候,基本就是思路错了,这个题也是看到一半觉得麻烦,然后发现果然思路错了。
public class Solution
{
public NestedInteger deserialize(String s)
{
if(s.length() == 0) return new NestedInteger();
Stack<NestedInteger> stk = new Stack<NestedInteger>();
int tempIndex = 0;
if(!s.contains("[")) return new NestedInteger(Integer.valueOf(s));
for(int i = 0; i < s.length();i++)
{
char tempCh = s.charAt(i);
if(tempCh == '[')
{
stk.push(new NestedInteger());
tempIndex = i + 1;
}
else if(tempCh == ',')
{
if( i != tempIndex)
{
int tempInt = Integer.valueOf(s.substring(tempIndex,i));
stk.peek().add(new NestedInteger(tempInt));
}
tempIndex = i + 1;
}
else if(tempCh == ']')
{
if( i != tempIndex)
{
int tempInt = Integer.valueOf(s.substring(tempIndex,i));
stk.peek().add(new NestedInteger(tempInt));
}
tempIndex = i + 1;
NestedInteger tempOB = stk.pop();
if(!stk.isEmpty()) stk.peek().add(tempOB);
else stk.push(tempOB);
}
// numbers
else
{
}
}
return stk.pop();
}
}
P.S. 有道云笔记各种崩溃,今天崩溃100次了,好像跟ALT有关。
385. Mini Parser的更多相关文章
- 【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 ...
- [LeetCode] Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- Leetcode: Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- LeetCode 385. Mini Parse
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- [Swift]LeetCode385. 迷你语法分析器 | Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- MvvmCross[翻译] 使用Xamarin与MvvmCross完成一个跨平台App
总览 原文:https://github.com/MvvmCross/MvvmCross/wiki/Tip-Calc-A-first-app 我们所做的第一个Model-View-ViewModel( ...
- Graphics类绘制图形
1. 画直线 void drawLine(int startX,int startY,int endX,int endY); 四个参数分别为:起始点的x坐标和y坐标以及终点的x坐标和y坐标,该方法用于 ...
- 数据库(学习整理)----4--Oracle数据查询(基础点1)
其他: 计算机中的内存是线性的,一维. length('')计算字符的个数,而不是字节的个数 Oracle中的日期类型和数值类型的数据可以做运算符(>,=,<,<>)比较 如果 ...
- Codevs 4768 跳石头 NOIP2015 DAY2 T1
4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 一年一度的"跳石头"比赛又要开始了! ...
- easyui-combobox小Demo
<script type="text/javascript"> $("#Function_TypeSelect").combobox({ onSel ...
- DB2 WIN7 WIN8在指示的文件系统中找不到数据库目录
前言:win7下一些软件的不正常,跟win7的权限有很大关系. 在win7下安装db2 9.7客户端后,在cmd中运行db2cmd启动clp,输入db2的任何命令都显示:SQL ...
- Node.js(window)基础(2)——node环境下的模块,模块间调用
参考:http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/00143450241959 ...
- In Place Upgrade of CentOS 6 to CentOS 7
Note: This is not the most highly recommended method to move from CentOS 6 to CentOS 7 ... but it ca ...
- 【技术贴】解决127.0.0.1和http://localhost均被拦截跳转到另一个网页
很艰难的历程. 今天安装一个OA系统,要用到http://127.0.0.1输入完成之后,可以进入安装界面,but,我输入完了之后,自动跳到了129129垃圾网站,艹,我真TM服了,我把本地连接网线都 ...
- Fibonacci Tree
hdu4786:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:给你一个无向图,然后其中有的边是白色的有的边是黑色的.然后问你是否存在一棵生成树,在 ...