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.
Analysis:
One function to parse a list; one function to parse an integer.
Solution:
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 if (s.length()==0) return null; 32 33 NestedInteger res; 34 if (s.charAt(0)=='['){ 35 res = new NestedInteger(); 36 parseList(s,0,res); 37 } else { 38 res = new NestedInteger(0); 39 parseInt(s,0,res); 40 } 41 42 return res; 43 } 44 45 // Parse a list starting from index @cur 46 public int parseList(String s, int cur, NestedInteger res){ 47 // list always start with '[', skip it 48 cur++; 49 50 while (s.charAt(cur)!=']'){ 51 if (s.charAt(cur)=='['){ 52 // next element is a list. 53 NestedInteger next = new NestedInteger(); 54 cur = parseList(s,cur,next); 55 res.add(next); 56 } else { 57 NestedInteger next = new NestedInteger(0); 58 cur = parseInt(s,cur,next); 59 res.add(next); 60 } 61 } 62 63 // skip ']' 64 cur++; 65 66 // skip the following ',' 67 if (cur<s.length() && s.charAt(cur) == ',') cur++; 68 69 return cur; 70 } 71 72 // Parse an integer typed NestedInteger starting from index @cur 73 public int parseInt(String s, int cur, NestedInteger res){ 74 boolean isNegative = false; 75 if (s.charAt(cur) == '-'){ 76 isNegative = true; 77 cur++; 78 } 79 80 int num = 0; 81 while (cur<s.length() && s.charAt(cur) >='0' && s.charAt(cur) <='9'){ 82 num = num*10+(s.charAt(cur) - '0'); 83 cur++; 84 } 85 if (isNegative){ 86 num = -num; 87 } 88 res.setInteger(num); 89 90 // Skip the following ',' 91 if (cur<s.length() && s.charAt(cur) == ',') cur++; 92 93 return cur; 94 } 95 }