private NestedInteger ParseList(string s, ref int index) { Debug.Assert(s[index] == '['); index++; // eat '[' NestedInteger root = new NestedInteger(); while (index < s.Length) { char c = s[index]; if (c == '[') { root.Add(ParseList(s, ref index)); } else if (('0' <= c && c <= '9') || c == '-') { root.Add(ParseNumber(s, ref index)); } else if (c == ',') { // skip index++; } else if (c == ']') { break; } else { throw new Exception(); } } Debug.Assert(s[index] == ']'); index++; // eat ']' return(root); }
// Set this NestedInteger to hold a nested list and adds a nested integer to it. public void Add(NestedInteger ni) { _value = null; _children.Add(ni); }