括号题一般都是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. 常见 PL.SQL 数据库操作

    Oracle PL/SQL 1, Alt +E 2, 默认大写功能, 解析SQL原则,Comment,UnComment. 3, 触发Trig,使用Test Window. 4, Compile In ...

  2. Sql Server内置函数实现MD5加密

    实例 MD5加密“123456”: HashBytes('MD5','123456') 结果:0xE10ADC3949BA59ABBE56E057F20F883E (提示:看完最后,结果要进行转换.) ...

  3. C# 处理图像三种方法对比

    C#本身自带有一定的图像处理能力,即使在不依赖Emgu CV的情况下,也是有很大的潜质的. 不过,最近在处理大量图片时,发现图片数量较少时,处理本身所带来的延时不会让人敏感,但是数量较大时,程序花费大 ...

  4. angularJS广播

    控制器之间共享数据(向父级/子级控制器传递event,data),类似于service在不同的控制器中通信 html: <div ng-controller="ParentCtrl&q ...

  5. 用3种方法在 operator= 中处理“自我赋值”

    假设你建立一个class 用来保存一个指针指向一块动态分配的位图. class Bitmap {......}; class Widget{ ... private: Bitmap* pb ; }; ...

  6. js快速排序法

    var quickSort = function(arr) { if (arr.length <= 1) { return arr; } var pivotIndex = Math.floor( ...

  7. 【python之旅】python简介和入门

    python简介: 一.什么是python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了打发时间,决心开发一个新的脚本解释程序, ...

  8. iOS 8创建交互式通知-备

    iOS 8提供了一个令人兴奋的新API来创建交互式通知(interactive notifications),它能让你在你的应用之外为用户提供额外的功能.我发现网上还没有关于如何实现它的比较好的示例教 ...

  9. nodejs发展

    http://www.infoq.com/cn/news/2012/11/netease-nodejs-framework http://www.jlmonteagudo.com/2013/06/ja ...

  10. Android开源项目发现--- 工具类图片缓存篇(持续更新)

    1. Android-Universal-Image-Loader 图片缓存 目前使用最广泛的图片缓存,支持主流图片缓存的绝大多数特性. 项目地址:https://github.com/nostra1 ...