题目:
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.分析1(递归实现-推荐):
/**
* // 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();
* }
*/
class Solution {
public NestedInteger deserialize(String s) {
//给定字符串,实现其特定返回类型
//思路:递归实现,对于属于同一层的直接添加,非同一层的递归将其中元素添加
NestedInteger nest=new NestedInteger();
//防止空串
if(s.length()==0||s==null) return nest;
if(s.charAt(0)!='['){
//防止只有数字的情况
nest.setInteger(Integer.parseInt(s));
return nest;
}
//空串[]
if(s.length()==2) {
return nest;
}
int start=1;
int layer=1;
for(int i=1;i<s.length();i++){
char ch=s.charAt(i);
if(layer==1&&(ch==','||i==s.length()-1)){
//递归实现其中的子集合
nest.add(deserialize(s.substring(start,i)));
start=i+1;
}
if(ch=='['){
//新的一层
layer++;
}else if(ch==']'){
layer--;
}
}
return nest;
}
}
分析2(stack实现-易理解):
/**
* // 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();
* }
*/
class Solution {
public NestedInteger deserialize(String s) {
//给定字符串,实现其特定返回类型
//思路:对整个字符串拆分需要判断当前是否是一个子集还是数字。同341使用stack
if(s.charAt(0)!='['){
//防止只有一个数字的情况
return new NestedInteger(Integer.valueOf(s));
}
//对字符串遍历
Stack<NestedInteger> stack=new Stack<NestedInteger>();
NestedInteger cur=null;
//用来表示标识对一个元素完成读取
int high=0;
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(ch=='['){
//当到下一个元素之前,判断前面一个元素是否满足(大的范围)
if(cur!=null){
stack.push(cur);
}
cur=new NestedInteger();
high=i+1;
}else if(ch==']'){
//对字符串截断
String str=s.substring(high,i);
//判断该字符串
if(!str.isEmpty()){
cur.add(new NestedInteger(Integer.valueOf(str)));
}
if(!stack.isEmpty()){
//同一个范围的,在一个NestedInteger集合中
NestedInteger pop=stack.pop();
pop.add(cur);
cur=pop;
}
high=i+1;
}else if(ch==','){
//判断是大范围的子集,还是小范围里面的子集
if(s.charAt(i-1)!=']'){
//大范围
String str=s.substring(high,i);
//直接添加进行
cur.add(new NestedInteger(Integer.valueOf(str)));
}
//重新修改游标
high=i+1;
}
}
return cur;
}
}