mini-parser
- 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的更多相关文章
- 【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]]]过程如下: # ...
- [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 ...
- [Swift]LeetCode385. 迷你语法分析器 | Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- 385 Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...
- 385. Mini Parser
括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode---String
Count and Say 思路:递归求出n - 1时的字符串,然后双指针算出每个字符的次数,拼接在结果后面 public String countAndSay(int n) { if(n == 1) ...
随机推荐
- Qt中两种定时器用法
在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度. 1.QObject类的定时器 QObj ...
- jQuery的data() 和 h5 的 dataset()
作用:获取自定义属性 命名规则:驼峰命名法 data-user==>user data-user-name==>userName 区别:jQuery:操作内存 h5: 操作DOM j ...
- Linux-数据库4
存储引擎 什么是存储引擎? mysql中建的库是文件夹,建的表是文件.文件有不同的类型,数据库中的表也有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎. 存储引擎说白了 ...
- 一个人也可以建立 TCP 连接呢
今天(恰巧是今天)看到有人在 SegmentFault 上问「TCP server 为什么一个端口可以建立多个连接?」.提问者认为 client 端就不能使用相同的本地端口了.理论上来说,确定一条链路 ...
- Java基础学习——多线程之线程池
1.线程池介绍 线程池是一种线程使用模式.线程由于具有空闲(eg:等待返回值)和繁忙这种不同状态,当数量过多时其创建.销毁.调度等都会带来开销.线程池维护了多个线程,当分配可并发执行的任务时, ...
- 洛谷P2746 USACO5.1 校园网
题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...
- JAVA读入挂
队友扒的uwi的读入挂,非常强,再也不用担心java比C++慢了-- import java.util.*; import java.math.*; import java.io.ByteArrayI ...
- macbook pro 开发帮助
java安装目录 /Library/java/JavaVirtualMachines/ 设置快捷目录 vim .bash_profile 文件中追加一下信息:export dirname=目录路径 重 ...
- PostgreSQL各命令行工具功能说明
I. SQL 命令 II. PostgreSQL 客户端应用 clusterdb -- 聚簇一个PostgreSQL数据库 createdb -- 创建一个新的PostgreSQL数据库 create ...
- Aptana studio 3配色方案的修改方法
http://www.weste.net/2013/1-29/88614.html Aptana studio 3的确是一个不错的IDE,但是默认的黑底白色代码样式看时间长了有点审美疲劳了,如何能够更 ...