Ejemplo n.º 1
0
        public string ToString(string format, IFormatProvider format_provider)
        {
            if (m_Value.HasValue)
            {
                return(m_Value.Value.ToString());
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("[");
                for (int index = 0; index < m_List.Count; index++)
                {
                    NestedInteger item = m_List[index];
                    if (index > 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(item.ToString(format, format_provider));
                }

                sb.Append("]");
                return(sb.ToString());
            }
        }
Ejemplo n.º 2
0
        public void Add(NestedInteger ni)
        {
            if (m_List == null)
            {
                m_List = new List <NestedInteger>();
            }

            m_List.Add(ni);
        }
Ejemplo n.º 3
0
        public int DepthSum(IList <NestedInteger> nestedList, int depth)
        {
            int sum = 0;

            for (int i = 0; i < nestedList.Count; i++)
            {
                NestedInteger item = nestedList[i];
                if (item.IsInteger())
                {
                    sum += depth * item.GetInteger();
                }
                else
                {
                    sum += DepthSum(item.GetList(), depth + 1);
                }
            }

            return(sum);
        }
Ejemplo n.º 4
0
        public int DepthSumInverse(IList <NestedInteger> nestedList)
        {
            List <int> sums = new List <int>();

            void backtrack(IList <NestedInteger> list, int depth)
            {
                if (sums.Count <= depth)
                {
                    sums.Add(0);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    NestedInteger item = list[i];
                    if (item.IsInteger())
                    {
                        sums[depth] += item.GetInteger();
                    }
                    else
                    {
                        backtrack(item.GetList(), depth + 1);
                    }
                }
            }

            backtrack(nestedList, 0);

            int sum = 0;

            for (int i = 0; i < sums.Count; i++)
            {
                sum += sums[i] * (sums.Count - i);
            }

            return(sum);
        }
Ejemplo n.º 5
0
        public static void Main()
        {
            Solution solution = new Solution();

            IList <NestedInteger> list;

            list = new NestedInteger[] { };

            Test.Check(solution.DepthSum, list, 0);

            list = new[] {
                new NestedInteger(1)
            };

            Test.Check(solution.DepthSum, list, 1);

            list = new[] {
                new NestedInteger(10),
                new NestedInteger(new NestedInteger[] {
                })
            };

            Test.Check(solution.DepthSum, list, 10);

            list = new[] {
                new NestedInteger(15),
                new NestedInteger(new[] {
                    new NestedInteger(new NestedInteger[] {
                    })
                })
            };

            Test.Check(solution.DepthSum, list, 15);

            list = new[] {
                new NestedInteger(15),
                new NestedInteger(new[] {
                    new NestedInteger(new[] {
                        new NestedInteger(1),
                        new NestedInteger(2),
                        new NestedInteger(new NestedInteger[] {
                        })
                    })
                })
            };

            Test.Check(solution.DepthSum, list, 24);

            list = new[] {
                new NestedInteger(new[] {
                    new NestedInteger(1),
                    new NestedInteger(1)
                }),
                new NestedInteger(2),
                new NestedInteger(new[] {
                    new NestedInteger(1),
                    new NestedInteger(1)
                })
            };

            Test.Check(solution.DepthSum, list, 10);

            list = new[] {
                new NestedInteger(1),
                new NestedInteger(new[] {
                    new NestedInteger(4),
                    new NestedInteger(new[] {
                        new NestedInteger(6)
                    })
                })
            };

            Test.Check(solution.DepthSum, list, 27);
        }