Given a nested list of integers, return the sum of all integers in the list weighted by their depth.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.

Example 1:
Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2)

Example 2:
Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)

这个题目实际上就是[LeetCode] 339. Nested List Weight Sum_Easy tag:DFS的变形, 没啥好说的, 用一个maxDepth来去得到最大的depth, 然后把339 的code拿来, depth递减即可.

Data Structure.

# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger(object):
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

Code

class Solution:
def NestedListWeightSum2(self, nestedList):
def maxDepth(nestedList, depth):
ans = 0
for each in nestedList:
if each.isInteger():
ans = max(ans, depth)
else:
ans = max(ans, maxDepth(each.getList(), depth + 1))
return ans
def helper(nestedList, depth):
s = 0
for each in nestedList:
if each.isInteger():
s += each.getInteger() * depth
else:
s += helper(each.getList(), depth -1)
return s
return helper(nestedList, maxDepth(nestedList, 1))

[LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS的更多相关文章

  1. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  2. [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  3. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  4. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  5. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  6. LeetCode 339. Nested List Weight Sum (嵌套列表重和)$

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  7. 364. Nested List Weight Sum II 大小反向的括号加权求和

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

  8. [leetcode]339. Nested List Weight Sum嵌套列表加权和

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  9. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...

随机推荐

  1. UDP协议的例子

    public class Service { // 服务器         public static void main(String[] args) {               Datagra ...

  2. .NET Core 2.2发布一览

    本周终于发布了.NET Core 2.2,ASP.NET Core 2.2以及Entity Framework Core 2.2,虽然更大的新闻可能是.NET Core 3.0的特性公布,但不妨先将现 ...

  3. .NET Core 中依赖注入 AutoMapper 小记

    最近在 review 代码时发现同事没有像其他项目那样使用 AutoMapper.Mapper.Initialize() 静态方法配置映射,而是使用了依赖注入 IMapper 接口的方式 servic ...

  4. ubuntu16.04安装 catkin_tools

    参考:https://catkin-tools.readthedocs.io/en/latest/installing.html First you must have the ROS reposit ...

  5. shell 基本命令

    Shell基本命令   前言 前面咱们已经成功安装了Linux系统--centos7,那么现在跟着超哥奔向Linux的大门. Linux命令行的组成结构 [root@oldboy_python ~]# ...

  6. {Django基础九之中间件} 一 前戏 二 中间件介绍 三 自定义中间件 四 中间件的执行流程 五 中间件版登陆认证

    Django基础九之中间件 本节目录 一 前戏 二 中间件介绍 三 自定义中间件 四 中间件的执行流程 五 中间件版登陆认证 六 xxx 七 xxx 八 xxx 一 前戏 我们在前面的课程中已经学会了 ...

  7. 【作业】DS稀疏矩阵

    写了两个小时,书上代码好难看啊 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #in ...

  8. cors 详解

    http://www.ruanyifeng.com/blog/2016/04/cors.html

  9. tomcat闪退

    tomcat/bin/setclasspath.bat 用记事本打开 在setclasspath.bat的文件里添加两行代码,告诉tomcat,jdk和jre的位置(添加位置在: rem Make s ...

  10. json序列化以及反序列化存在多个对象时候的处理

    存在多个对象的时候,只需要将反序列化存在的对象,遍历出来即可. using System;using System.Collections.Generic;using System.Linq;usin ...