LeetCode 385. Mini Parse

时间:2021-07-07 10:40:09

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代码】

 /**
* // 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) {
Stack<NestedInteger> sk = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if(ch == '[') {
sk.push(new NestedInteger());
}
else if((ch >= '0' && ch <= '9') || ch == '-') {
sb.append(ch);
}
else {
if(sb.length() >= 1) {
int num = Integer.parseInt(sb.toString());
NestedInteger ni = new NestedInteger(num);
NestedInteger top = sk.peek();
top.add(ni);
sb.delete(0, sb.length());
}
if(ch == ']') {
NestedInteger top = sk.pop();
if(sk.isEmpty()) return top;
NestedInteger newtop = sk.peek();
newtop.add(top);
}
}
}
if (sb.length() >= 1)
return new NestedInteger(Integer.parseInt(sb.toString()));
return sk.peek();
}
}