Ejemplo n.º 1
0
        // Set this NestedInteger to hold a nested list and adds a nested integer to it.
        public void Add(NestedInteger ni)
        {
            if (List == null)
            {
                List = new List <NestedInteger>();
            }

            List.Add(ni);
        }
Ejemplo n.º 2
0
        public NestedInteger Solution(string s)
        {
            NestedInteger res = new NestedInteger();

            res.Add(new NestedInteger(123));

            NestedInteger res2 = new NestedInteger();

            res2.SetInteger(456);
            res2.Add(new NestedInteger(789));

            res.Add(res2);


            var i    = 0;
            var res3 = Helper(s, ref i);

            return(res3);
        }
Ejemplo n.º 3
0
        public NestedInteger Helper(string s, ref int index)
        {
            NestedInteger res = new NestedInteger();

            var  num  = 0;
            bool flag = false;

            for (; index < s.Length; index++)
            {
                if (s[index] == '[')
                {
                    if (flag)
                    {
                        res.Add(Helper(s, ref index));
                    }
                    else
                    {
                        flag = true;
                    }
                }
                else if (s[index] >= '0' && s[index] <= '9')
                {
                    num = num * 10 + (s[index] - '0');
                    res.SetInteger(num);
                }
                else if (s[index] == ']')
                {
                    return(res);
                }
                else
                {
                    res.Add(new NestedInteger(num));
                    num = 0;
                }
            }

            return(res);
        }