Question
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:
- An integer containing value 123.
- 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.
Code
/**
* // 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) {
if (s.isEmpty()) return null;
if (s.charAt(0) != '[') return new NestedInteger(Integer.valueOf(s));
Stack<NestedInteger> stack = new Stack<>();
NestedInteger curr = null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '[') {
if (curr != null) stack.push(curr);
curr = new NestedInteger();
} else if (c == ']') {
if (sb.length() > 0) {
curr.add(new NestedInteger(Integer.valueOf(sb.toString())));
sb.delete(0, sb.length());
}
if (!stack.isEmpty()) {
NestedInteger ni = stack.pop();
ni.add(curr);
curr = ni;
}
} else if (c == ',') {
if (s.charAt(i - 1) != ']') {
curr.add(new NestedInteger(Integer.valueOf(sb.toString())));
sb.delete(0, sb.length());
}
} else {
sb.append(c);
}
}
return curr;
}
}
Solution
http://m.blog.csdn.net/article/details?id=52259424
遇到’[‘字符肯定是要產生一個新的 NestedInteger 對象的。
遇到’]’字符則表明上一個元素可以結束了,此時要處理這里面的整型字符串,將其解析成int值再傳給當前的NestedInteger對象。并且呢,由于當前元素已經結束解析,還需要將它傳給它的父NestedInteger。
遇到’,’字符要分情況了,如果它的前一個字符是’]’則表明在步驟2種已經做了處理了,否則的話說明之前的整型字符串還沒有解析。
如果遇到了0到9還有-,則暫時不作處理,將其拼接到一個StringBuilder里面。
我們這里再來拿一個字符串來討論看看,對于字符串”[-1,[123],[[3]]]”
首先遇到’[‘產生一個NestedInteger,對應著最外層的NestedInteger, 記作NI1,并賦值給curNi(NI1);
接著向后遍歷,直到遇到了第一個’,’,此時要為前面的整型值’-1’實例化一個NestedInteger對象,并插入到最外層的curNi(NI1);
繼續向后遍歷,遇到第二個’[‘,先將curNi(NI1)壓入stack中,再實例化一個新的NestedInteger對象,記作NI2,且令賦值給curNi(NI2);
向后遍歷,遇到第二個’[‘所對應的’]’,為前面的整型值’123’實例化一個NestedInteger對象,add進curNI(NI2)中。再彈出stack中的NI1對象,將curNI(NI2)add到NI中,再令curNi = NI1,注意此時stack中已空;
繼續,遇到第二個’,’但是發現它的前一個字符是’]’,不作處理;
繼續遍歷,遇到第三個’[‘,先將curNI(NI1)壓入stack中。再實例化一個新的NestedInteger對象,記作NI3,令curNI = NI3;
繼續遍歷,遇到第四個’[‘,先將curNI(NI3)壓入stack中。再實例化一個新的NestedInteger對象,記作NI4,令curNI = NI4;
繼續遍歷,遇到第四個’[‘所對應的’]’,為’3’實例化一個NestedInteger對象,插入到curNI(NI4)中。從stack中彈出NI3,將curNI(NI4)插入到NI3中,且令curNI = NI3;
繼續遍歷,遇到第三個’[‘所對應的’]’,前面沒有未處理的整型字符串。此時stack里面還有一個NI1,彈出NI1,將curNI(NI3)add給NI1,且令curNI = NI1;
到了最后一個’]’,也對應了第一個’]’,此時stack為空,且沒有未處理的字符串了。此時,curNI就對應了最外層的那個NestedInteger,結束。