Esempio n. 1
0
        public int DepthSumInverse(IList <NestedInteger> nestedList)
        {
            Queue <NestedInteger> q = new Queue <NestedInteger>();
            int total = 0, preTotal = 0;

            foreach (NestedInteger ni in nestedList)
            {
                q.Enqueue(ni);
            }

            while (q.Count > 0)
            {
                int qSize      = q.Count;
                int levelTotal = 0;

                for (int i = 0; i < qSize; i++)
                {
                    NestedInteger temp = q.Dequeue();
                    if (temp.IsInteger())
                    {
                        levelTotal += temp.GetInteger();
                    }
                    else
                    {
                        foreach (NestedInteger x in temp.GetList())
                        {
                            q.Enqueue(x);
                        }
                    }
                }

                preTotal += levelTotal;
                total    += preTotal;
            }

            return(total);
        }
Esempio n. 2
0
        public int DepthSum(IList <NestedInteger> nestedList)
        {
            Queue <NestedInteger> q = new Queue <NestedInteger>();
            int total = 0;
            int level = 1;

            foreach (NestedInteger ni in nestedList)
            {
                q.Enqueue(ni);
            }

            while (q.Count > 0)
            {
                int qSize = q.Count;

                for (int i = 0; i < qSize; i++)
                {
                    NestedInteger temp = q.Dequeue();
                    if (temp.IsInteger())
                    {
                        total += (level * temp.GetInteger());
                    }
                    else
                    {
                        foreach (NestedInteger x in temp.GetList())
                        {
                            q.Enqueue(x);
                        }
                    }
                }

                level++;
            }

            return(total);
        }