这个题做了一个多小时,好傻逼。

显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层。。

用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和顶端的距离来判定层数,但是其实最后POP的过程相当于又遍历了一次。而且STACK无法O(1) access,换成LIST需要手动来维持顺序。

最终放弃了,看答案。。发现都是先遍历一次得到最高权重。。。。。。仔细想想似乎1-PASS可以用MAP实现,但是每次发现新高权重,就必须更新以前所有的,貌似没必要1PASS。。。

如果不要求1PASS,先遍历算权重的话,这个题就很直白了。。

public class Solution
{
int depth;
public int depthSumInverse(List<NestedInteger> nestedList)
{
if(nestedList.size() == 0) return 0; depth = getDepth(nestedList); return helper(nestedList,1);
} public int getDepth(List<NestedInteger> list)
{
int res = 1; for(NestedInteger n: list)
if(!n.isInteger())
res = Math.max(res,getDepth(n.getList())+1); return res;
} public int helper(List<NestedInteger> list, int curLevel)
{
int res = 0;
for(NestedInteger i: list)
if(i.isInteger())
{
res += (depth+1-curLevel)*i.getInteger();
}
else
{
res +=helper(i.getList(),curLevel+1);
} return res;
} }


二刷。

这个题也有印象,一刷的时候尝试1-PASS的DFS,根本不可能。

BFS倒是可以,模拟level order traversal。很重要的一点就是要保留积累值,代码里我用的cum= =baby cum..cum..

重点是。。每到新的一层,返还结果都加一遍积累值,就可以满足权重weighted的关系。最早的我想法乘,DFS找到权重然后乘第几层,但是实际上这里反倒是返璞归真,加法最适合这种方式,而乘法根本难以表示这种关系。。

总共N层,最上面的一层作为积累制存在的N次,总共被加到res里N次,正好是这个题的意思。。

public class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if (nestedList.size() == 0) return 0;
int res = 0;
int cum = 0;
List<NestedInteger> tempList = new LinkedList<>();
while (true) {
for (NestedInteger i : nestedList) {
if (i.isInteger()) {
cum += i.getInteger();
} else {
tempList.addAll(i.getList());
}
}
res += cum;
if (tempList.size() == 0) {
return res;
} else {
nestedList = tempList;
tempList = new ArrayList<>();
}
}
}
}

所以结果是可以1-pass的。

DFS就先走一遍,找到最深的层数,然后递归并添加一个当前参数代表当前层数,层数差表示他出现的次数。

意淫完回头一看一刷,就是这么做的,我真是毫无长进。。。

364. Nested List Weight Sum II的更多相关文章

  1. [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. ...

  2. [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. ...

  3. LeetCode 364. Nested List Weight Sum II

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

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

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

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

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

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

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

  7. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

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

  8. Leetcode: Nested List Weight Sum II

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

  9. 嵌套列表的加权和 · Nested List Weight Sum

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

随机推荐

  1. SVM(支持向量机)算法

    第一步.初步了解SVM 1.0.什么是支持向量机SVM 要明白什么是SVM,便得从分类说起. 分类作为数据挖掘领域中一项非常重要的任务,它的目的是学会一个分类函数或分类模型(或者叫做分类器),而支持向 ...

  2. ACM HDU 2674 N! Again(数论)

    继续数论.. Problem Description WhereIsHeroFrom:            Zty,what are you doing ? Zty:                 ...

  3. sql知识

    SQL 基本知识 SQL Server 是Microsoft 公司推出的关系型数据库管理系统.具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的 ...

  4. web版扫雷小游戏(四)

    ~~~接上篇,游戏的主体框架完成了,接下来我们对游戏中存在的两个主要实体进行分析,一个是雷点类BombObject(节点对象),一个是节点对象对应的图片对象BombImgObject,根据第一篇的介绍 ...

  5. PHPCMS标签大全

    {$head[title]} 页面标题,用法: {$phpcms[sitename]} 网站名称 用法: {$head[keywords]} 要害字 用法: {$head[description]} ...

  6. linux无法umount解决方案

    [root@995120-master ~]# umount /drbd/ umount: /drbd: device is busy.(In some cases useful info about ...

  7. Start an installation from GRUB

    Start an installation from GRUB Description This tip will show you how to start an installation for ...

  8. JSP 结构

    网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面. 容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP的正 ...

  9. mvc Model元数据【学习笔记】

    页面中Html.Editorfor(model=>model.fieldname)这些方法,都是通过Model的元数据来生成html的,我们如果想控制最终生成的html,可以通过修改元数据来实现 ...

  10. Hbase Java API程序设计步骤

    http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象 包含了客户端链接Hbase服务所需的全部信息: zoo ...