https://leetcode.com/problems/mini-parser/

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class Solution {
public NestedInteger deserialize(String s) {
if (s.equals("")) {
return new NestedInteger();
}
if (s.startsWith("[")) {
return impl(s.substring(1, s.length()-1));
}
return new NestedInteger(Integer.parseInt(s));
} private NestedInteger impl(String s) {
if (s.equals("")) {
return new NestedInteger();
}
char [] chList = s.toCharArray();
int wrap = 0;
int left = 0;
NestedInteger ret = new NestedInteger();
for (int i = 0; i < chList.length; i++) {
if (chList[i] == '[') {
wrap++;
}
else if (chList[i] == ']') {
wrap--;
}
else if (chList[i] == ',' && wrap == 0) {
ret.add(deserialize(s.substring(left, i)));
left = i + 1;
}
}
ret.add(deserialize(s.substring(left, chList.length)));
return ret;
}
}

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. [LeetCode] Mini Parser 迷你解析器

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

  4. Leetcode: Mini Parser

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

  5. [Swift]LeetCode385. 迷你语法分析器 | Mini Parser

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

  6. 385 Mini Parser 迷你解析器

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

  7. 385. Mini Parser

    括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  10. LeetCode---String

    Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...

随机推荐

  1. Eclipse中syso 快捷键 Alt + / 不能使用的问题

    通过使用windows-preferences-java-editor-templates中的快捷键,可以显著提升输入速度.快捷键的设置一般是在这里以及general下面的keys里面设置. 但是,在 ...

  2. Entity Framework Core介绍(1)

    介绍 Entity Framework (EF) Core 是轻量化.可扩展和跨平台版的常用 Entity Framework 数据访问技术. EF Core 可用作对象关系映射程序 (O/RM),以 ...

  3. Sublime Text 下的Install Package安装方法

    废话不多说.... 如果你是Sublime Text3用户,按下Ctrl+~呼出控制台,输入以下代码 import urllib.request,os,hashlib; h = '7183a2d3e9 ...

  4. 【转】高效率编辑器VIM

    最近实习的时候需要在服务器上做Debug,不得不用到vim的相关操作.以前对vim这种被码农无数赞扬的神器望而却步,但今天试了之后感觉还是不错的.以后争取少用鼠标,少用insert模式. 这是从网上看 ...

  5. [BZOJ4700]适者(CDQ分治+DP/李超线段树)

    如果没有秒杀,就是经典的国王游戏问题,按t/a从小到大排序即可. 考虑删除两个数i<j能给答案减少的贡献:S[i]*T[i]+P[i-1]*A[i]-A[i]+S[j]*T[j]+P[j-1]* ...

  6. Codeforces Round #489 (Div. 2)

    A. Nastya and an Array time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Socket通信原理简介

    Socket通信原理简介 字数1011 阅读1766 评论2 喜欢11 何谓socket 计算机,顾名思义即是用来做计算.因而也需要输入和输出,输入需要计算的条件,输出计算结果.这些输入输出可以抽象为 ...

  8. webwork或Struts配置网站根路径的默认页面办法

    参考资料:http://www.iteye.com/problems/24028 查阅好多资料,关于webwork或Struts处理默认页面的方式,能否像spring MVC那样直接指定默认访问页面. ...

  9. 河南省队选拔 HAOI2015 解题报告

      其实省选在四天前就已经结束了,但由于题目难度略大我到今天上午才补完所有题目……(捂脸逃)考场上很幸运,打完了所有我会写的部分分,最后Round1的110分 + Round2的70分,勉强算是没有被 ...

  10. wikioi 1080 线段树练习 树状数组

    1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现 ...