LeetCode 385. Mini Parser

原题链接在这里:https://leetcode.com/problems/mini-parser/description/

题目:

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

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Note: You may assume that the string is well-formed:

  • String is non-empty.
  • String does not contain white spaces.
  • String contains only digits 0-9[- ,].

Example 1:

Given s = "324",

You should return a NestedInteger object which contains a single integer 324.

Example 2:

Given s = "[123,[456,[789]]]",

Return a NestedInteger object containing a nested list with 2 elements:

1. An integer containing value 123.
2. A nested list containing two elements:
    i.  An integer containing value 456.
    ii. A nested list with one element:
         a. An integer containing value 789.

题解:

遇到'[', 说明要往下走一层. 把current NestedInteger push进stack, 创建个新的.

遇到']', 说明本层走完, 要往上走一层. pop出上层NestedInteger, 吧current NestedInteger 加到 上层NestedInteger里.

遇到",", 如果前面不是"]", 说明是当前层NestedInteger加上个Integer.

l 是一段数字开始的index. r是一段数字结束的index+1.

如果开始就不是"[", 特例说明这就是一个单层的数.

Time Complexity: O(s.length()).

Space: O(s.length()). stack space.

AC Java:

 1 /**
 2  * // This is the interface that allows for creating nested lists.
 3  * // You should not implement it, or speculate about its implementation
 4  * public interface NestedInteger {
 5  *     // Constructor initializes an empty nested list.
 6  *     public NestedInteger();
 7  *
 8  *     // Constructor initializes a single integer.
 9  *     public NestedInteger(int value);
10  *
11  *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
12  *     public boolean isInteger();
13  *
14  *     // @return the single integer that this NestedInteger holds, if it holds a single integer
15  *     // Return null if this NestedInteger holds a nested list
16  *     public Integer getInteger();
17  *
18  *     // Set this NestedInteger to hold a single integer.
19  *     public void setInteger(int value);
20  *
21  *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
22  *     public void add(NestedInteger ni);
23  *
24  *     // @return the nested list that this NestedInteger holds, if it holds a nested list
25  *     // Return null if this NestedInteger holds a single integer
26  *     public List<NestedInteger> getList();
27  * }
28  */
29 class Solution {
30     public NestedInteger deserialize(String s) {
31         if(s.charAt(0) != '['){
32             return new NestedInteger(Integer.valueOf(s));
33         }
34         
35         NestedInteger cur = null;
36         Stack<NestedInteger> stk = new Stack<NestedInteger>();
37         
38         int l = 0;
39         for(int r = 0; r<s.length(); r++){
40             char c = s.charAt(r);
41             if(c == '['){
42                 if(cur != null){
43                     stk.push(cur);
44                 }
45                 cur = new NestedInteger();
46                 l = r+1;
47             }else if(c == ']'){
48                 String num = s.substring(l, r);
49                 if(num.length() != 0){
50                     cur.add(new NestedInteger(Integer.valueOf(num)));
51                 }
52                 if(!stk.isEmpty()){
53                     NestedInteger top = stk.pop();
54                     top.add(cur);
55                     cur = top;
56                 }
57                 l = r+1;
58             }else if(c == ','){
59                 if(s.charAt(r-1) != ']'){
60                     String num = s.substring(l, r);
61                     if(num.length() != 0){
62                         cur.add(new NestedInteger(Integer.valueOf(num)));
63                     }
64                 }
65                 l = r+1;
66             }
67         }
68         
69         return cur;
70     }
71 }

类似Flatten Nested List Iterator.

posted @ 2017-10-25 13:11  Dylan_Java_NYC  阅读(228)  评论(0编辑  收藏  举报