364. Nested List Weight Sum II
这个题做了一个多小时,好傻逼。
显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层。。
用了好久是因为想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的更多相关文章
- [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. ...
- [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. ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- 364. Nested List Weight Sum II 大小反向的括号加权求和
[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [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. ...
- [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. ...
- 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. ...
- 嵌套列表的加权和 · Nested List Weight Sum
[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...
随机推荐
- 关于for循环中的闭包问题
还是昨天的那个简单的小项目,已经花了一天的时间了 - - .从&&的用法,到CSStext,到今天马上要谈的闭包(closure),通过一个小东西,真真发现了自己的各方面不足.昨天发完 ...
- ACM hdu 1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- php利用时间生成随机函数
date("YmdHis",time()); rand(); 生成随机数 当括号内无参数时 系统会以当前时间为种子进行随机数的生成 rand(1,10); 括号里面是生 ...
- Node.js(window)基础(1)——用cmd命令行访问某一文件夹下的js文件
一.安装,从官网上下载安装,安装基本一直点击下一步就行.注意:node.js基于Python的,安装node.js之前电脑上要安装Python,最好是Python2.7或2.6. 二.cmd进入命令行 ...
- System.Reflection.Assembly.GetEntryAssembly()获取的为当前已加载的程序集
今天在使用System.Reflection.Assembly.GetEntryAssembly()获取程序集时,发现获取的程序集不全.原来是因为C#的程序集为延迟加载,此方法只获取当前已加载的,未加 ...
- C++ 利用socket实现TCP,UDP网络通讯
学习孙鑫老师的vc++深入浅出,有一段时间了,第一次接触socket说实话有点儿看不懂,第一次基本上是看他说一句我写一句完成的,第二次在看SOCKET多少有点儿感觉了,接下来我把利用SOCKET完成T ...
- 《Braid》碎片式台词
谁见到过风? 你没有,我也没有. 但当树儿低下头, 便是风儿经过时. 便是风儿穿过的时候. 但当树叶微微摇首, 你没有,我也没有. 谁见到过风? 二.时间与宽恕 1.提姆要出发了!他要去寻找并救出公主 ...
- MFC 仿QQ聊天软件(黄花寒)
http://blog.csdn.net/lh844386434/article/details/6655080 http://download.csdn.net/download/lh8443864 ...
- JS仿淘宝详情页菜单条智能定位效果
类似于淘宝详情页菜单条智能定位 对于每个人来说并不陌生!如下截图所示:红色框的那部分! 基本原理: 是用JS侦听滚动事件,当页面的滚动距离(页面滚动的高度)大于或者等于 "对象"( ...
- log4j_slf4j log4j.properties
hibernate 使用的日志是slf4j,而 slf4j又有各种实现策略. 使用log4j 就是其中一种方式. 需要的jar 包: log4j-1.2.16.jar slf4j-api-1.6.1. ...