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总的的边数减去重叠的 ...
随机推荐
- C# 之【获取网页】
C#获取指定网页HTML原代码可使用 WebClient WebRequest HttpWebRequest 三种方式来实现. 当然也可使用webBrowse!在此就不研究webBrowse如何获取了 ...
- 使用ArrayList对大小写字母的随机打印
从a~z以及A~Z随机生成一个字母并打印:打印全部的字母 package com.liaojianya.chapter1; import java.util.ArrayList; /** * This ...
- QT UI 如果发现布局之后,button不在父widget的中间
如果发现布局之后,button不在父widget的中间: 调整父widget的布局参数:
- Android Material Design NavigationView 及 Palette 颜色提取器
DrawerLayout + NavigationView DrawerLayout布局,通常在里面添加两个子控件,程序主界面添加到NavitagionView前面. <android.supp ...
- 网站开发常用jQuery插件总结(四)验证插件validation
在网站开发过程中,有时我们需要验证用户输入的信息是否符合我们的要求,所以我们会对用户提交的数据进行验证.验证分两次进行,一次是在客户端,一次是在服务端.客户端的验证可以提升用户的体验. jquery验 ...
- oracle系列索引
今天终于把oracle入门的知识通篇过了一遍. 一篇文章没有写,先做个索引.把知识系统的梳理下. 数据库基本概念-oracle介绍 oracle安装,配置,启动 oracle工具 sqlplus 用户 ...
- File System Shell
Overview appendToFile cat chgrp chmod chown copyFromLocal copyToLocal count cp du dus expunge get ge ...
- Installing MySQL Server
Installing MySQL Server Here we will learn how to Compile and Install the MySQL Server from source c ...
- Bag of Words/Bag of Features的Matlab源码发布
2010年11月19日 ⁄ 技术, 科研 ⁄ 共 1296字 ⁄ 评论数 26 ⁄ 被围观 4,150 阅读+ 由于自己以前发过一篇文章讲bow特征的matlab代码的优化的<Bag-Of-Wo ...
- For Aisha(阿伊莎)
相见时难别亦难,东风无力百花残.by:昂思多,20160524 跟你在一起,没有拘束感,完全就像是在跟亲人对话. 很喜欢这种感觉 虽然才认识不到10天,却就像是认识了好几年的老朋友 真的喜欢叫你“阿伊 ...