括号题一般都是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的更多相关文章

  1. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  2. 385. Mini Parser - LeetCode

    Question 385. Mini Parser Solution 分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]过程如下: # ...

  3. 385 Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...

  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. Python:列表

    #!/usr/bin/python3 #列表 是可变的,可修改的 listDemo = ["one","two","three"," ...

  2. ENC28J60 + M430G2553,用uip搭建http服务器,解决“在XP系统下可以访问,在Win7下不能访问”的问题

    近日,用ENC28J60,在M430G2553上搭建一个简单的HTTP服务器,结果发现在XP系统下可以访问,在Win7下不能访问,非常奇葩的问题. 通过抓包,如下图,计算机(IP地址为192.168. ...

  3. Java学习----字符串函数

    1.String string类是最终类,不可以有子类 package com.demo.pkg1; public class TestString { public static void main ...

  4. WebMethod 属性

    WebMethod有以下几种属性: BufferResponse CacheDuration Description EnableSession MessageName TransactionOpti ...

  5. 几个模式识别和计算机视觉相关的Matlab工具箱

    模式识别.计算机视觉.图像处理等领域大部分是对一些图像等数据的处理,比较常用的语言是C++和Matlab,相应也对应很多库,象opencv等,都是很好用功能也很强大,但是对于数据处理更方便的应该还是M ...

  6. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

      The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 151 ...

  7. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  8. python 运行 hadoop 2.0 mapreduce 程序

    要点:#!/usr/bin/python 因为要发送到各个节点,所以py文件必须是可执行的. 1) 统计(所有日志)独立ip数目,即不同ip的总数 ####################本地测试## ...

  9. 利用jquery操作Radio方法小结

    用Radio来实现用户的选择效果,在项目中积累了一些利用JQUERY来操作Radio的方法,这里与大家分享下 在开发中经常会用到Radio来实现用户的选择效果,我在项目中积累了一些利用JQUERY来操 ...

  10. Contest 高数题 樹的點分治 樹形DP

    高数题 HJA最近在刷高数题,他遇到了这样一道高数题.这道高数题里面有一棵N个点的树,树上每个点有点权,每条边有颜色.一条路径的权值是这条路径上所有点的点权和,一条合法的路径需要满足该路径上任意相邻的 ...