Exemplo n.º 1
0
        private int GetSum(NestedInteger nestedIntegerInput, int depth)
        {
            int result = 0;
            LinkedList <(NestedInteger, int)> queue = new LinkedList <(NestedInteger, int)>();

            queue.AddLast((nestedIntegerInput, depth));

            while (queue.Count != 0)
            {
                (NestedInteger ni, int cDepth) = queue.First.Value;
                queue.RemoveFirst();

                if (ni.IsInteger())
                {
                    result += ni.GetInteger() * cDepth;
                }
                else
                {
                    foreach (var item in ni.GetList())
                    {
                        queue.AddLast((item, cDepth - 1));
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        private int GetDepth(NestedInteger nesteInteger)
        {
            int maxDepth = 1;
            LinkedList <(NestedInteger, int)> queue = new LinkedList <(NestedInteger, int)>();

            queue.AddLast((nesteInteger, maxDepth));

            while (queue.Count != 0)
            {
                (NestedInteger ni, int depth) = queue.First.Value;
                queue.RemoveFirst();

                if (ni.IsInteger())
                {
                    maxDepth = Math.Max(maxDepth, depth);
                }
                else
                {
                    foreach (var item in ni.GetList())
                    {
                        queue.AddLast((item, depth + 1));
                    }
                }
            }

            return(maxDepth);
        }
Exemplo n.º 3
0
        // Set this NestedInteger to hold a nested list and adds a nested integer to it.
        public void Add(NestedInteger ni)
        {
            if (IsInteger() || values == null)
            {
                value  = null;
                values = new List <NestedInteger>();
            }

            values.Add(ni);
        }