Mini Parser
Given a nested list of Integers as a string, implement a parser to deserilize it.
Each element is either a string or a list whose element can be integers or other list.
String is not empty and only contains [, ], -, 1-9, ','
e.g: s = "234" -> return a NestedInteger object that only contains a single number 234.
s = "[123,[456, [789]]]" -> return a NestedInteger object contains 123 and another NestedInteger b.
b contains 456 and another NesedInteger c.
c contains only a single number 789.
The Interface 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(); * } */
Solution:
1. We use a StringBuilder to store the temporary number. We use a Stack to store previous NestedInteger. Scan the whole String.
2. Whenever we encounter a digit or '-', we add it to the StringBuilder.
3. Whenever we encounter a '[', we initialize a NestedInteger and push it to the stack.
4. Whenever we encounter a ',', and the length of the StringBuilder is not 0, we put the String value of the StringBuilder to the top NestedInteger of the Stack. Then clear the StringBuilder. Because there can be "[[12],[23]]" so that we should consider the length of the StringBuilder.
5. Whenever we encounter a ']' and the length of the StringBuilder is not 0, we put the String value of the StringBuilder to the top NestedInteger of the Stack. Then clear the StringBuilder. If the length of the StringBuilder is 0, we pop the Stack as the result and if the stack is not empty, we add the result to the top of the stack.
6. return the result.
Code:
public class Solution { public NestedInteger deserialize(String s) { if(s.length()==0) return new NestedInteger(); if(s.charAt(0)!='[') return new NestedInteger(Integer.parseInt(s)); Stack<NestedInteger> st = new Stack<>(); StringBuilder sb = new StringBuilder(); NestedInteger result = null; for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); if(c=='['){ st.push(new NestedInteger()); }else if(c=='-' || Character.isDigit(c)){ sb.append(c); }else if(c==','){ if(sb.length()!=0){ st.peek().add(new NestedInteger(Integer.valueOf(sb.toString()))); sb.delete(0,sb.length()); } }else{ if(sb.length()!=0){ st.peek().add(new NestedInteger(Integer.valueOf(sb.toString()))); sb.delete(0,sb.length()); } result = st.pop(); if(!st.empty()){ st.peek().add(result); } } } return result; } }

浙公网安备 33010602011771号