LeetCode 385. Mini Parse

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.

Subscribe to see which companies asked this question


【题目分析】

用String的形式给定一个嵌套的list,然后实现对这个字符串的反序列化。给定的字符串满足下列要求:

1. 非空

2. 不包含空格

3. 只包含0-9,[,],- 这几种字符


【思路】

对于嵌套的形式,我们要把一对中括号括起来的内容嵌套进中括号外面的内容中。

我们使用栈来保存之前的内容,当前一对中括号的内容如果构造完毕,就把它嵌套进栈中之前的一个元素。为此我们需要做以下几件事情:

1. 遍历字符串

2. 如果当前字符是'['则新建一个NestedInteger入栈

3. 如果当前字符是0-9或者-,则加入到当前数字字符串序列中

4. 如果当前字符是','或者']',如果数字字符串非空,则把该数字加入到栈顶的NestedInteger中,如果当前字符是']'则还要把栈顶元素弹出并添加到新的栈顶元素中

5. 遍历结束后如果最后的数字序列非空,则把该数字加入栈顶元素并返回栈顶元素,否则直接返回栈顶元素。


【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 public class Solution {
30     public NestedInteger deserialize(String s) {
31         Stack<NestedInteger> sk = new Stack<>();
32         StringBuilder sb = new StringBuilder();
33         for (int i = 0; i < s.length(); i++) {
34             char ch = s.charAt(i);
35             if(ch == '[') {
36                 sk.push(new NestedInteger());
37             }
38             else if((ch >= '0' && ch <= '9') || ch == '-') {
39                 sb.append(ch);
40             }
41             else {
42                 if(sb.length() >= 1) {
43                     int num = Integer.parseInt(sb.toString());
44                     NestedInteger ni = new NestedInteger(num);
45                     NestedInteger top = sk.peek();
46                     top.add(ni);
47                     sb.delete(0, sb.length());
48                 }
49                 if(ch == ']') {
50                     NestedInteger top = sk.pop();
51                     if(sk.isEmpty()) return top;
52                     NestedInteger newtop = sk.peek();
53                     newtop.add(top);
54                 }
55             }
56         }
57         if (sb.length() >= 1) 
58             return new NestedInteger(Integer.parseInt(sb.toString()));
59         return sk.peek();
60     }
61 }
原文地址:https://www.cnblogs.com/liujinhong/p/6039596.html