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.
需要一层一层剥开来构造NestedInteger,如果s不带有括号,例如s="324",s="573,829"等形式,表明NestedInteger没有内嵌,直接以逗号分割解析成int类型去构造单一的NestedInteger并塞入当前NestedInteger即可。如果s带有括号,表明至少有一个内嵌NestedInteger,需要区分一下s="324"和s="[324]"的情况,前者表示单一的NestedInteger,其数值为324,后者表示一个NestedInteger没有值,只含有一个内嵌的NestedInteger对象,内嵌对象的值为324.
s带括号,先把最外层的括号去除,例如"[123,[456,336,[789]]]",第一步变成"123,[456,336,[789]]",这样用不带括号的部分分别构建NestedInteger,并塞入当前NestedInteger,其余部分递归构造一个复合的NestedInteger,然后同样塞入当前NestedInteger
取出递归构造的部分“[456,336,[789]]”需要栈的帮助,遇到“[” 入栈,遇到"]"出栈,如果当前出栈导致栈空的话就把栈弹空,这样就找到了相应的最左边的"["匹配,把这一部分作为参数递归传递。
另外数字的分割以“,”为分隔符,如果当前栈空,两个","之间的数字应该拿出来构造一个单独的NestedInteger并塞入当前NestedInteger,如果栈不空,则表明正在检索复合部分,忽略分割操作。
/**
* // 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)
{
int len=s.length();
Stack<Integer> stack=new Stack<>();
int start=0,end=len-1;
if(s.charAt(0)=='[')
{
start=1;
end=len-2;
}
else {
return new NestedInteger(Integer.parseInt(s));
}
NestedInteger nestint=new NestedInteger();
int last=start-1,now=start;
int left=0;
for(int i=start;i<=end;i++)
{
char c=s.charAt(i);
if(c==',')
{
if(left==0)
{
now=i;
if(last+1!=now)
nestint.add(new NestedInteger(Integer.parseInt(s.substring(last+1,now))));
last=now;
}
else {
}
}
else if (c=='[')
{
stack.push(i);
left++;
}
else if(c==']')
{
left--;
if(left==0)
{
while(stack.size()>1)
stack.pop();
nestint.add(deserialize(s.substring(stack.pop(), i+1)));
last=i;
}
}
}
if(last!=end)
nestint.add(new NestedInteger(Integer.parseInt(s.substring(last+1,end+1))));
return nestint;
}
}