括号题一般都是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. CSS 尺寸 (Dimension)

    CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度.同样,它允许你增加行间距. 更多实例 设置元素的高度 这个例子演示了如何设置不同元素的高度. 使用百分比设置图像的高度 这个例子演 ...

  2. java动态绑定的情况分析

    java是面向对象的语言,java中多态的一种情况是动态绑定.所谓的动态绑定,分两种情况:当调用类方法的时候,java虚拟机会基于对象的引用类型来选择执行方法.当java调用一个实例方法的时候,他会根 ...

  3. [转]jQuery源码分析系列

    文章转自:jQuery源码分析系列-Aaron 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://github.com/JsAaro ...

  4. C语言中short的意思

    short和int等一样,是C或C++的一种内部数据类型.用于表示有符号整数.不同的是,他们在内存中所占的空间大小不同,short通常为int所占一半,也有一些实现为和int一样,但不会比int大.所 ...

  5. apache的ab进行页面的压力测试

    参考http://www.cnblogs.com/yjf512/archive/2011/05/24/2055723.html apache/bin/ab ./ab –n 1000 –c 100 ht ...

  6. javascript删除目标标签

    <script> window.onload=function(){ var odiv=document.getElementById("content");  // ...

  7. ART模式和Dalvik模式的异同

    Dalvik模式 如果要解释清楚什么是ART模式,我们就需要从Android系统的应用编译模式说起,我们都知道Android系统是以Linux系统为底层构建的,Android系统是开源(源代码公开)的 ...

  8. Installing MySQL Server

    Installing MySQL Server Here we will learn how to Compile and Install the MySQL Server from source c ...

  9. css3:user-select属性

    一.user-select简介 这是在css3 UI规范中新增的一个功能,用来控制内容的可选择性 二.user-select:值 auto——默认值,用户可以选中元素中的内容 none——用户不能选择 ...

  10. matlab常用小函数(一)

    (第1维为对每一列操作,第2维维对每一行操作) sum 求和操作 max 求最大值操作 sum:求和操作 sum(A):矩阵A按列向求和(每一列求和),结果为一个行向量 sum(A,2):矩阵A按行向 ...