Esempio n. 1
0
        private int CalculateSum(NestedInteger input)
        {
            int result = 0;
            LinkedList <(NestedInteger, int)> queue = new LinkedList <(NestedInteger, int)>();

            queue.AddLast((input, 1));

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

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

            return(result);
        }
Esempio n. 2
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);
        }