题目:
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; 13 + 42 + 6*1 = 17)

解答:
这一题其实挺tricky的,如果说第一道题的关键是记录层次,那么这一题的关键是把这一层的integer sum传到下一层去,代码如下:

public int DFS(List<NestedInteger> nestedList, int intSum) {//关键点在于把上一层的integer sum传到下一层去,这样的话,接下来还有几层,每一层都会加上这个integer sum,也就等于乘以了它的层数List<NestedInteger> nextLevel = new ArrayList<>();int listSum = 0;for (NestedInteger list : nestedList) {if (list.isInteger()) {intSum += list.getInteger();} else {nextLevel.addAll(list.getList());}}listSum = nextLevel.isEmpty() ? 0 : DFS(nextLevel, intSum);return listSum + intSum;
}public int depthSumInverse(List<NestedInteger> nestedList) {return DFS(nestedList, 0);
}

364. Nested List Weight SumII相关推荐

  1. 364. Nested List Weight Sum II

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

  2. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  3. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

  4. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  6. LinkedIn TAG

    1 [leetcode]243. Shortest Word Distance最短单词距离 Two Pointers 2 [leetcode]244. Shortest Word Distance I ...

  7. LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本(二)

    前面一篇由于文字太多,不给编辑,遂此篇出炉 LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本(一) DFS&BFS (262)200. Number of Islan ...

  8. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  9. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

最新文章

  1. mysql怎样删除上一行_mysql如何删除第一行数据
  2. 一根Express Route同时支持ARM和ASM的VNET
  3. python环境变量的配置 alias_vim-python环境配置
  4. 智能指针weak_ptr
  5. 送给水深火热的 Gopher 们的解药
  6. svr公式推导_支持向量回归(SVR)的详细介绍以及推导算法
  7. 扫地机器人什么牌子好?专业人士推荐这五款高端产品
  8. WPF MVVC 基础 - 父子窗体
  9. php ==gt;,谈谈PHP中的 -gt;、=gt; 和 :: 符号 - 易采站长站
  10. 屏幕录像软件Community Clips Recorder简介及其使用技巧
  11. mac能开发android吗,怎么在Mac上开发Android应用
  12. 第二章 SPSS 的数据管理
  13. redis中hget命令
  14. matlab写子函数,matlab主函数子函数怎么写
  15. android 页面回滚焦点位置 解决方案
  16. pta 03-树1 树的同构 SDUT 3340 数据结构实验之二叉树一:树的同构
  17. 企业宣传类PPT模板
  18. bugkuctf 游戏通关玄学式速通
  19. 软件测试遵循的基本原则
  20. 方向比努力重要 能力比知识重要 健康比成绩重要 生活比文凭重要 情商比智商重要

热门文章

  1. 【C语言基础】gdb调试工具的使用
  2. win8能开发android的sdk么,Win8下Android SDK安装与环境变量配置教程
  3. 三星s10能升级android11,三星 S10+手机已在测试 Android 11 系统
  4. python format格式化输出填充符号不起作用_Python格式化输出——format用法示例
  5. mysql2800_mysql error 1045 (2800):。。 'root'@'localhost' (using password:YES)
  6. 如何知道linux的ssh秘钥是否匹配,SSH密钥验证
  7. Oracle中获取文件中的数据,操作oracle中的数据文件
  8. html 正则表达式验证金额,js金额校验,js正则表达式,包含正负,小数点后两位...
  9. linux13位时间戳,Kotlin 处理Linux时间戳
  10. android 图片分析,Android图片处理实例分析