[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.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Different from the [leetcode]339. Nested List Weight Sum嵌套列表加权和 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:
Input: [[1,1],2,[1,1]]
Output: 8
Explanation: Four 1's at depth 1, one 2 at depth 2.
Example 2:
Input: [1,[4,[6]]]
Output: 17
Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17.
思路
跑的最快的方法是 DFS
1. We can observe that
1x + 2y + 3z = (x+y+z) * (3+1) - (3x+2y+1z)
^ levelSum ^maxDepth ^ Nested List Weight Sum I problem
2. Use DFS recursion, converting this problem to Nested List Weight Sum I, updating levelSum and maxPath at the same time when using DFS
代码
class Solution {
int levelSum = 0;
int maxDepth = 1;
public int depthSumInverse(List<NestedInteger> nestedList) {
int depthSum = dfs(nestedList, 1);
return levelSum * (maxDepth + 1) - depthSum;
}
private int dfs(List<NestedInteger> nestedList, int depth) {
int sum = 0;
for (NestedInteger n : nestedList) {
if (n.isInteger()) {
// same as Nested List Weight Sum I
sum += n.getInteger() * depth;
// at the same time, use DFS to update levelSum and maxDepth
maxDepth = Math.max(depth, maxDepth);
levelSum += n.getInteger();
} else {
// same as Nested List Weight Sum I
sum += dfs(n.getList(), depth + 1);
}
}
return sum;
}
}
思路
最容易想到的方法 DFS
1. use helper function to get maxDepth
2. same as Nested List Weight Sum I, use dfs function to get result. Only concerning that do substraction instead of addition when entering next new level
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null || nestedList.size() == 0) return 0;
int depth = helper(nestedList);
int sum = dfs(nestedList, depth);
return sum;
}
// helper recursion function to get the maxDepth
public int helper(List<NestedInteger> nestedList) {
int depth = 0;
for (NestedInteger n : nestedList) {
if(n.isInteger()) {
depth = Math.max(depth, 1);
}
else {
depth = Math.max(depth, helper(n.getList()) + 1);
}
}
return depth;
}
// same as Nested List Weight Sum I
public int dfs(List<NestedInteger> nestedList, int depth) {
int result = 0;
for (NestedInteger n : nestedList) {
if (n.isInteger()) {
result += n.getInteger() * depth;
} else {
result += dfs(n.getList(), depth - 1);
}
}
return result;
}
}
思路
BFS(level order traversal)
if we want to get 3x + 2y + 1z, we can use preSum tech like that
levelSum x
preSum x
result x
=======================
levelSum x y
preSum x x+y
result x x + x + y
=======================
levelSum x y z
preSum x x+y x+y+z
result x x + x + y x + x + y + x + y + z
代码
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null || nestedList.size() == 0) return 0;
// initialize
int preSum = 0;
int result = 0;
// put each item of list into the queue
Queue<NestedInteger> queue = new LinkedList<>(nestedList);
while(!queue.isEmpty()){
//depends on different depth, queue size is changeable
int size = queue.size();
int levelSum = 0;
for(int i = 0; i < size; i++){
NestedInteger n = queue.poll();
if(n.isInteger()){
levelSum += n.getInteger();
}
else{
// depends on different depth, queue size is changeable
queue.addAll(n.getList());
}
}
preSum += levelSum;
result += preSum;
}
return result;
}
}
[leetcode]364. Nested List Weight Sum II嵌套列表加权和II的更多相关文章
- 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. ...
- [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
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- [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]339. Nested List Weight Sum嵌套列表加权和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://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 ...
- 364. Nested List Weight Sum II
这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...
随机推荐
- Centos7.3 之mysql5.7二进制安装
#!/bin/bash #注意,该脚本是在centos7.3非生产环境下测试的,其他版本的系统可能不适用,要根据情况修改.需要先下载好mysql二进制包到本地(我一般都是在root家目录下操作,文件也 ...
- ssh登录原理及免密登录方法
免密登录设置 1.进入到我的home目录 cd ~/.ssh 2.生成ssh免登陆秘钥ssh-keygen -t rsa (四个回车) 执行完这个命令后,会生成两个文件id_rsa(私钥) ...
- python(二)——list、字典、字符串操作
列表——list 假设一种场景,想要存班里所有同学的名字,那应该拿什么存呢? 我们可以拿一个字符串去存,例如: stus = "s1,s2,s3,s4,s5……sn" 那我们要从里 ...
- Java高级特性 第6节 注解初识
一.注解概述 Java注解也就是Annotation,是Java代码里的特殊标记,它为Java程序代码提供了一种形式化的方法,用来表达额外的某些信息,这些信息是代码本身无法表示的. 注解以标签的形式存 ...
- delphi XML简单处理
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- Firebug 没死,活在 Firefox DevTools 中
伯乐在线转注:2016年12月7日有一条<Firebug 宣布停止开发更新>的资讯,不少朋友误认为以后用不到 Firebug 了.其实在 2015 年 Firebug 已经在着手整合到 F ...
- 第六届蓝桥杯省赛 java三羊献瑞
将文字看作一个个变量.根据一开始确定的文字的值进行暴力循环. 三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞------------------- 三 羊 生 瑞 气 (如果有对齐 ...
- 前端-JavaScript1-3——JavaScript之字面量
字面量?????? 字面量:英语叫做literals,有些书上叫做直接量.看见什么,它就是什么. 我们先来学习数字的字面量,和字符串的字面量.剩余的字面量类型,我们日后遇见再介绍. 3.1 数字的字面 ...
- WPF DEV dxc:ChartControl 柱状图
先上效果图: <UserControl xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" mc ...
- tomcat advanced (RUNNING)
1. 1. tomcat