1. https://leetcode.com/problems/mini-parser/
  2.  
  3. /**
  4. * // This is the interface that allows for creating nested lists.
  5. * // You should not implement it, or speculate about its implementation
  6. * public interface NestedInteger {
  7. * // Constructor initializes an empty nested list.
  8. * public NestedInteger();
  9. *
  10. * // Constructor initializes a single integer.
  11. * public NestedInteger(int value);
  12. *
  13. * // @return true if this NestedInteger holds a single integer, rather than a nested list.
  14. * public boolean isInteger();
  15. *
  16. * // @return the single integer that this NestedInteger holds, if it holds a single integer
  17. * // Return null if this NestedInteger holds a nested list
  18. * public Integer getInteger();
  19. *
  20. * // Set this NestedInteger to hold a single integer.
  21. * public void setInteger(int value);
  22. *
  23. * // Set this NestedInteger to hold a nested list and adds a nested integer to it.
  24. * public void add(NestedInteger ni);
  25. *
  26. * // @return the nested list that this NestedInteger holds, if it holds a nested list
  27. * // Return null if this NestedInteger holds a single integer
  28. * public List<NestedInteger> getList();
  29. * }
  30. */
  31. public class Solution {
  32. public NestedInteger deserialize(String s) {
  33. if (s.equals("")) {
  34. return new NestedInteger();
  35. }
  36. if (s.startsWith("[")) {
  37. return impl(s.substring(1, s.length()-1));
  38. }
  39. return new NestedInteger(Integer.parseInt(s));
  40. }
  41.  
  42. private NestedInteger impl(String s) {
  43. if (s.equals("")) {
  44. return new NestedInteger();
  45. }
  46. char [] chList = s.toCharArray();
  47. int wrap = 0;
  48. int left = 0;
  49. NestedInteger ret = new NestedInteger();
  50. for (int i = 0; i < chList.length; i++) {
  51. if (chList[i] == '[') {
  52. wrap++;
  53. }
  54. else if (chList[i] == ']') {
  55. wrap--;
  56. }
  57. else if (chList[i] == ',' && wrap == 0) {
  58. ret.add(deserialize(s.substring(left, i)));
  59. left = i + 1;
  60. }
  61. }
  62. ret.add(deserialize(s.substring(left, chList.length)));
  63. return ret;
  64. }
  65. }

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. Qt中两种定时器用法

    在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度. 1.QObject类的定时器 QObj ...

  2. jQuery的data() 和 h5 的 dataset()

    作用:获取自定义属性 命名规则:驼峰命名法  data-user==>user data-user-name==>userName 区别:jQuery:操作内存   h5: 操作DOM j ...

  3. Linux-数据库4

    存储引擎 什么是存储引擎? mysql中建的库是文件夹,建的表是文件.文件有不同的类型,数据库中的表也有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎. 存储引擎说白了 ...

  4. 一个人也可以建立 TCP 连接呢

    今天(恰巧是今天)看到有人在 SegmentFault 上问「TCP server 为什么一个端口可以建立多个连接?」.提问者认为 client 端就不能使用相同的本地端口了.理论上来说,确定一条链路 ...

  5. Java基础学习——多线程之线程池

    1.线程池介绍     线程池是一种线程使用模式.线程由于具有空闲(eg:等待返回值)和繁忙这种不同状态,当数量过多时其创建.销毁.调度等都会带来开销.线程池维护了多个线程,当分配可并发执行的任务时, ...

  6. 洛谷P2746 USACO5.1 校园网

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...

  7. JAVA读入挂

    队友扒的uwi的读入挂,非常强,再也不用担心java比C++慢了-- import java.util.*; import java.math.*; import java.io.ByteArrayI ...

  8. macbook pro 开发帮助

    java安装目录 /Library/java/JavaVirtualMachines/ 设置快捷目录 vim .bash_profile 文件中追加一下信息:export dirname=目录路径 重 ...

  9. PostgreSQL各命令行工具功能说明

    I. SQL 命令 II. PostgreSQL 客户端应用 clusterdb -- 聚簇一个PostgreSQL数据库 createdb -- 创建一个新的PostgreSQL数据库 create ...

  10. Aptana studio 3配色方案的修改方法

    http://www.weste.net/2013/1-29/88614.html Aptana studio 3的确是一个不错的IDE,但是默认的黑底白色代码样式看时间长了有点审美疲劳了,如何能够更 ...